|
7 | 7 | import tools.jackson.databind.json.JsonMapper; |
8 | 8 | import io.github.easy4j.opencli.OpenCliProperties; |
9 | 9 | import io.github.easy4j.opencli.core.OpenCliOutputParser; |
| 10 | +import io.github.easy4j.opencli.core.OpenCliRemoteExecutionDetails; |
| 11 | +import io.github.easy4j.opencli.core.OpenCliRemoteExecutionDetails.ItemsSource; |
| 12 | +import io.github.easy4j.opencli.core.OpenCliRemoteExecutionDetails.Outcome; |
10 | 13 | import io.github.easy4j.opencli.core.OpenCliResult; |
11 | 14 | import io.github.easy4j.opencli.exception.OpenCliExecutableFailureException; |
12 | 15 | import io.github.easy4j.opencli.exception.OpenCliNonZeroExitException; |
@@ -139,9 +142,10 @@ public OpenCliResult collect(OpenCliCollectRequest request, long submittedAtNano |
139 | 142 | if (status < 200 || status >= 300) { |
140 | 143 | log.warn("Agent HTTP non-2xx correlationId={} status={} responseChars={}", |
141 | 144 | correlationId, status, respBody.length()); |
142 | | - throw failure("Agent HTTP " + status, correlationId); |
| 145 | + throw new OpenCliExecutableFailureException("Agent HTTP " + status + " correlationId=" + correlationId, |
| 146 | + null, failureSnapshot(status, Outcome.HTTP_FAILURE, respBody)); |
143 | 147 | } |
144 | | - return mapResponse(respBody, correlationId); |
| 148 | + return mapResponse(status, respBody, correlationId); |
145 | 149 | } catch (JacksonException e) { |
146 | 150 | log.warn("Agent response parse failed correlationId={}", correlationId); |
147 | 151 | throw failure("Failed to parse agent response", correlationId); |
@@ -232,63 +236,75 @@ private static OpenCliExecutableFailureException failure(String message, String |
232 | 236 | return new OpenCliExecutableFailureException(message + " correlationId=" + correlationId, null); |
233 | 237 | } |
234 | 238 |
|
235 | | - private OpenCliResult mapResponse(String respBody, String correlationId) { |
| 239 | + private OpenCliResult mapResponse(int status, String respBody, String correlationId) { |
236 | 240 | String rawCapture = captureRawIfEnabled(respBody); |
237 | 241 | JsonNode envelope; |
238 | 242 | try { |
239 | 243 | envelope = ENVELOPE_READER.readTree(respBody); |
240 | 244 | } catch (JacksonException e) { |
241 | 245 | log.warn("Agent response envelope parse failed correlationId={} responseChars={}", |
242 | 246 | correlationId, respBody.length()); |
243 | | - throw new OpenCliRemoteProtocolException("Failed to parse agent response envelope", correlationId); |
| 247 | + throw new OpenCliRemoteProtocolException("Failed to parse agent response envelope", correlationId, |
| 248 | + failureSnapshot(status, Outcome.PROTOCOL_FAILURE, respBody)); |
244 | 249 | } |
245 | 250 | if (Objects.isNull(envelope) || !envelope.isObject() || !envelope.path("success").isBoolean()) { |
246 | 251 | log.warn("Agent response envelope status invalid correlationId={} responseChars={}", |
247 | 252 | correlationId, respBody.length()); |
248 | | - throw new OpenCliRemoteProtocolException("Invalid agent response envelope status", correlationId); |
| 253 | + throw new OpenCliRemoteProtocolException("Invalid agent response envelope status", correlationId, |
| 254 | + failureSnapshot(status, Outcome.PROTOCOL_FAILURE, respBody)); |
249 | 255 | } |
250 | 256 | JsonNode error = envelope.path("error"); |
251 | 257 | if (!error.isMissingNode() && !error.isNull() && !error.isTextual()) { |
252 | 258 | log.warn("Agent response envelope error field invalid correlationId={} responseChars={}", |
253 | 259 | correlationId, respBody.length()); |
254 | | - throw new OpenCliRemoteProtocolException("Invalid agent response envelope error field", correlationId); |
| 260 | + throw new OpenCliRemoteProtocolException("Invalid agent response envelope error field", correlationId, |
| 261 | + failureSnapshot(status, Outcome.PROTOCOL_FAILURE, respBody)); |
255 | 262 | } |
256 | 263 | boolean success = envelope.path("success").booleanValue(); |
257 | 264 | String err = error.isTextual() ? error.asText() : ""; |
258 | 265 | JsonNode items = envelope.path("items"); |
259 | 266 | String stdout; |
| 267 | + ItemsSource itemsSource; |
260 | 268 | if (items.isMissingNode() || items.isNull()) { |
261 | 269 | stdout = "[]"; |
262 | | - } else if (items.isArray()) { |
263 | | - stdout = MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(items); |
| 270 | + itemsSource = items.isMissingNode() ? ItemsSource.ABSENT_DEFAULT : ItemsSource.NULL_DEFAULT; |
264 | 271 | } else { |
265 | | - stdout = items.toPrettyString(); |
| 272 | + stdout = items.isArray() ? MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(items) |
| 273 | + : items.toPrettyString(); |
| 274 | + itemsSource = ItemsSource.RESPONSE; |
266 | 275 | } |
267 | 276 | OpenCliParsedFields parsed = OpenCliOutputParser.parseBestEffort(stdout, err); |
268 | | - if (!success) { |
269 | | - log.warn( |
270 | | - "Agent collect reported failure correlationId={} errorChars={} stdoutLength={}", |
271 | | - correlationId, |
272 | | - err.length(), |
273 | | - stdout.length()); |
274 | | - OpenCliResult failed = |
275 | | - OpenCliResult.builder() |
276 | | - .stdout(stdout) |
277 | | - .stderr(err) |
278 | | - .exitCode(1) |
279 | | - .success(false) |
280 | | - .parsed(parsed) |
281 | | - .remoteRawHttpBody(rawCapture) |
282 | | - .build(); |
283 | | - throw new OpenCliNonZeroExitException("OpenCLI agent reported failure correlationId=" + correlationId, failed); |
284 | | - } |
285 | | - return OpenCliResult.builder() |
| 277 | + OpenCliResult result = OpenCliResult.builder() |
286 | 278 | .stdout(stdout) |
287 | | - .stderr(err.isEmpty() ? "" : err) |
288 | | - .exitCode(0) |
289 | | - .success(true) |
| 279 | + .stderr(err) |
| 280 | + .exitCode(success ? 0 : 1) |
| 281 | + .success(success) |
290 | 282 | .parsed(parsed) |
291 | 283 | .remoteRawHttpBody(rawCapture) |
| 284 | + .remoteExecutionDetails(OpenCliRemoteExecutionDetails.builder() |
| 285 | + .httpStatus(status) |
| 286 | + .legacySuccess(success) |
| 287 | + .outcome(success ? Outcome.SUCCESS : Outcome.COMMAND_FAILURE) |
| 288 | + .itemsSource(itemsSource) |
| 289 | + .build()) |
| 290 | + .build(); |
| 291 | + if (!success) { |
| 292 | + log.warn("Agent collect reported failure correlationId={} errorChars={} stdoutLength={}", |
| 293 | + correlationId, err.length(), stdout.length()); |
| 294 | + throw new OpenCliNonZeroExitException("OpenCLI agent reported failure correlationId=" + correlationId, result); |
| 295 | + } |
| 296 | + return result; |
| 297 | + } |
| 298 | + |
| 299 | + private OpenCliResult failureSnapshot(int status, Outcome outcome, String respBody) { |
| 300 | + return OpenCliResult.builder() |
| 301 | + .success(false) |
| 302 | + .remoteRawHttpBody(captureRawIfEnabled(respBody)) |
| 303 | + .remoteExecutionDetails(OpenCliRemoteExecutionDetails.builder() |
| 304 | + .httpStatus(status) |
| 305 | + .outcome(outcome) |
| 306 | + .itemsSource(ItemsSource.UNAVAILABLE) |
| 307 | + .build()) |
292 | 308 | .build(); |
293 | 309 | } |
294 | 310 |
|
|
0 commit comments