|
| 1 | +package io.github.easy4j.opencli.parser; |
| 2 | + |
| 3 | +import tools.jackson.core.json.JsonFactory; |
| 4 | +import tools.jackson.core.StreamReadConstraints; |
| 5 | +import tools.jackson.core.StreamReadFeature; |
| 6 | +import tools.jackson.databind.DeserializationFeature; |
| 7 | +import tools.jackson.databind.ObjectReader; |
| 8 | +import tools.jackson.databind.json.JsonMapper; |
| 9 | +import io.github.easy4j.opencli.parser.OpenCliErrorEnvelope.Format; |
| 10 | +import io.github.easy4j.opencli.parser.OpenCliErrorEnvelope.Status; |
| 11 | +import io.github.easy4j.opencli.util.OpenCliStrings; |
| 12 | +import java.nio.charset.StandardCharsets; |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.Collections; |
| 15 | +import java.util.LinkedHashMap; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.Objects; |
| 19 | + |
| 20 | +/** Decodes local CLI failure metadata without changing process status or interpreting advice. */ |
| 21 | +public final class OpenCliErrorDecoder { |
| 22 | + private static final int MAX_BYTES = 256 * 1024; |
| 23 | + private static final int MAX_DEPTH = 32; |
| 24 | + private static final ObjectReader JSON = new JsonMapper(JsonFactory.builder() |
| 25 | + .enable(StreamReadFeature.STRICT_DUPLICATE_DETECTION) |
| 26 | + .streamReadConstraints(StreamReadConstraints.builder().maxNestingDepth(MAX_DEPTH).build()).build()) |
| 27 | + .readerFor(Object.class) |
| 28 | + .with(DeserializationFeature.FAIL_ON_TRAILING_TOKENS, DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS); |
| 29 | + |
| 30 | + private OpenCliErrorDecoder() { |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @param stderr captured error text |
| 35 | + * @param observedExit observed local process exit code, or null |
| 36 | + * @param incomplete true if transport or lifecycle prevented a complete local observation |
| 37 | + * @return decoded metadata or a status describing why it is unavailable |
| 38 | + */ |
| 39 | + public static OpenCliErrorEnvelope decode(String stderr, Integer observedExit, boolean incomplete) { |
| 40 | + OpenCliErrorEnvelope.OpenCliErrorEnvelopeBuilder result = |
| 41 | + OpenCliErrorEnvelope.builder().observedProcessExit(observedExit); |
| 42 | + if (incomplete) { |
| 43 | + return result.status(Status.INCOMPLETE).build(); |
| 44 | + } |
| 45 | + if (Objects.isNull(stderr) || stderr.isEmpty()) { |
| 46 | + return result.status(Status.EMPTY).build(); |
| 47 | + } |
| 48 | + if (stderr.length() > MAX_BYTES || stderr.getBytes(StandardCharsets.UTF_8).length > MAX_BYTES) { |
| 49 | + return result.status(Status.OVER_BUDGET).build(); |
| 50 | + } |
| 51 | + int first = 0; |
| 52 | + while (first < stderr.length() && " \t\r\n".indexOf(stderr.charAt(first)) >= 0) { |
| 53 | + first++; |
| 54 | + } |
| 55 | + if (first == stderr.length()) { |
| 56 | + return result.status(Status.EMPTY).build(); |
| 57 | + } |
| 58 | + boolean json = stderr.charAt(first) == '{' || stderr.charAt(first) == '['; |
| 59 | + result.format(json ? Format.JSON : Format.YAML); |
| 60 | + try { |
| 61 | + Object decoded = json ? JSON.readValue(stderr) : OpenCliErrorYaml.read(stderr, MAX_DEPTH, MAX_BYTES); |
| 62 | + if (!(decoded instanceof Map)) { |
| 63 | + return result.status(Status.NO_ENVELOPE).build(); |
| 64 | + } |
| 65 | + Map<String, Object> root = immutableMap((Map<?, ?>) decoded); |
| 66 | + if (!Boolean.FALSE.equals(root.get("ok")) || !(root.get("error") instanceof Map)) { |
| 67 | + return result.status(Status.NO_ENVELOPE).build(); |
| 68 | + } |
| 69 | + Map<String, Object> error = asMap(root.get("error")); |
| 70 | + Object code = error.get("code"); |
| 71 | + Object message = error.get("message"); |
| 72 | + Object help = error.get("help"); |
| 73 | + Object exit = error.get("exitCode"); |
| 74 | + Object trace = root.get("trace"); |
| 75 | + if (!(code instanceof String) || OpenCliStrings.isBlank((String) code) |
| 76 | + || !(message instanceof String) || !(exit instanceof Integer) |
| 77 | + || (Objects.nonNull(help) && !(help instanceof String)) |
| 78 | + || (Objects.nonNull(trace) && !(trace instanceof Map))) { |
| 79 | + return result.status(Status.INVALID).build(); |
| 80 | + } |
| 81 | + return result.status(Status.DECODED).code((String) code).message((String) message) |
| 82 | + .help((String) help).reportedExit((Integer) exit).envelopeFields(root).errorFields(error) |
| 83 | + .trace(Objects.isNull(trace) ? Collections.emptyMap() : asMap(trace)).build(); |
| 84 | + } catch (RuntimeException ex) { |
| 85 | + return result.status(Status.INVALID).build(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @SuppressWarnings("unchecked") |
| 90 | + private static Map<String, Object> asMap(Object value) { |
| 91 | + return (Map<String, Object>) value; |
| 92 | + } |
| 93 | + |
| 94 | + private static Map<String, Object> immutableMap(Map<?, ?> source) { |
| 95 | + Map<String, Object> result = new LinkedHashMap<>(); |
| 96 | + source.forEach((key, value) -> { |
| 97 | + if (!(key instanceof String)) { |
| 98 | + throw new IllegalArgumentException("Envelope keys must be strings"); |
| 99 | + } |
| 100 | + result.put((String) key, immutableValue(value)); |
| 101 | + }); |
| 102 | + return Collections.unmodifiableMap(result); |
| 103 | + } |
| 104 | + |
| 105 | + private static Object immutableValue(Object value) { |
| 106 | + if (value instanceof Map) { |
| 107 | + return immutableMap((Map<?, ?>) value); |
| 108 | + } |
| 109 | + if (value instanceof List) { |
| 110 | + List<Object> result = new ArrayList<>(); |
| 111 | + ((List<?>) value).forEach(item -> result.add(immutableValue(item))); |
| 112 | + return Collections.unmodifiableList(result); |
| 113 | + } |
| 114 | + return value; |
| 115 | + } |
| 116 | +} |
0 commit comments