Files
baiyue-firstleg/common-web/src/main/java/com/baiyue/common/web/EventDeliveryObservation.java
T

31 lines
1.4 KiB
Java

package com.baiyue.common.web;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.baiyue.common.event.EventIdentifiers;
import feign.FeignException;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.Set;
/** Diagnostic identity and finite audit error codes; never serialize exception messages or payloads. */
public final class EventDeliveryObservation {
private EventDeliveryObservation() { }
public static String eventId(ObjectMapper mapper, String json) {
try {
String id = mapper.readTree(json).path("eventId").asText("");
return EventIdentifiers.isSupported(id) ? id : null;
} catch (Exception invalid) { return null; } // Invalid payloads are still audited by Outbox entry ID.
}
public static String errorCode(Throwable error, boolean permanent, boolean requestStarted) {
Set<Throwable> seen = Collections.newSetFromMap(new IdentityHashMap<>());
for (Throwable cause = error; cause != null && seen.add(cause); cause = cause.getCause()) {
if (cause instanceof feign.codec.DecodeException) return "INVALID_RESPONSE";
if (cause instanceof FeignException http && http.status() >= 100 && http.status() <= 599) {
return "HTTP_" + http.status();
}
}
if (!requestStarted) return "INVALID_EVENT";
return permanent ? "PERMANENT_FAILURE" : "DELIVERY_FAILURE";
}
}