Skip to content

Instantly share code, notes, and snippets.

@Opalo
Last active November 27, 2018 16:26
Show Gist options
  • Save Opalo/7c9a17429bcc862cbffe1a2a9e5966c2 to your computer and use it in GitHub Desktop.
Save Opalo/7c9a17429bcc862cbffe1a2a9e5966c2 to your computer and use it in GitHub Desktop.
@Slf4j
@Component
class DomainExceptionWrapper extends DefaultErrorAttributes {
private final ReqTracer tracer;
DomainExceptionWrapper(ReqTracer tracer) {
super(false);
this.tracer = tracer;
}
@Override
public Map<String, Object> getErrorAttributes(ServerRequest request,
boolean includeStackTrace) {
final var error = getError(request);
final var errorAttributes = super.getErrorAttributes(request, false);
errorAttributes.put(ErrorAttribute.TRACE_ID.value, tracer.traceId());
if (error instanceof DomainException) {
log.debug("Caught an instance of: {}, err: {}", DomainException.class, error);
final var errorStatus = ((DomainException) error).getStatus();
errorAttributes.replace(ErrorAttribute.STATUS.value, errorStatus.value());
errorAttributes.replace(ErrorAttribute.ERROR.value, errorStatus.getReasonPhrase());
return errorAttributes;
}
return errorAttributes;
}
enum ErrorAttribute {
STATUS("status"),
ERROR("error"),
TRACE_ID("traceId");
private final String value;
ErrorAttribute(String value) {
this.value = value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment