fix(event-bridge): retain downstream business errors

This commit is contained in:
2026-08-24 16:59:02 +08:00
parent 8e12dd9a38
commit 3019f5da77
2 changed files with 48 additions and 2 deletions
@@ -45,11 +45,31 @@ public class HttpLastlegEventClient implements LastlegEventClient {
.POST(HttpRequest.BodyPublishers.ofString(body))
.build();
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
JsonNode responseBody = objectMapper.readTree(response.body());
JsonNode responseBody;
try {
responseBody = objectMapper.readTree(response.body());
} catch (Exception ignored) {
responseBody = null;
}
if (response.statusCode() / 100 != 2 || responseBody == null
|| !"200".equals(responseBody.path("code").asText())) {
throw new IllegalStateException("尾程消费失败: HTTP " + response.statusCode());
throw new IllegalStateException(failureMessage(response.statusCode(), responseBody));
}
return String.valueOf(response.statusCode());
}
static String failureMessage(int statusCode, JsonNode body) {
String code = body == null ? "missing" : body.path("code").asText("missing");
String message = body == null ? "non-json response" : body.path("message").asText("missing");
String requestId = body == null ? "missing" : body.path("requestId").asText("missing");
String traceId = body == null ? "missing" : body.path("traceId").asText("missing");
return "尾程消费失败: HTTP " + statusCode + ", code=" + bounded(code)
+ ", message=" + bounded(message) + ", requestId=" + bounded(requestId)
+ ", traceId=" + bounded(traceId);
}
private static String bounded(String value) {
String safe = value == null || value.isBlank() ? "missing" : value.replaceAll("[\\r\\n]+", " ");
return safe.length() <= 240 ? safe : safe.substring(0, 240);
}
}