Search code examples
javaquarkusresteasy

Add custom data to 500 server errors responses


I am trying to modify the responses returned in case of uncontrolled errors (status code >= 500), in this case I want to add a correlation ID to the response for easier error log aggregation in a distributed application. For status codes <= 499 this works fine with @ServerResponseFilter

@ServerResponseFilter
public void modifyResponse(ContainerResponseContext responseContext) {
    Log.debugf("Calling modifyResponse with context %s", responseContext);
    if (responseContext.getStatus() >= 400) {
        if (responseContext.getEntity() instanceof String) {
            responseContext.setEntity("Error with Correlation-ID " + MDC.get("correlation_id") + ": " + responseContext.getEntity());
        }
    }
}

However, modifyResponse is not invoked for responses with status codes >= 500, even when thrown "manually" using e.g. throw new WebApplicationException("abort!", 500).

Is there a way I can enrich the HTTP response with some custom data in case of 500 server errors?


Solution

  • What you are seeing is actually the expected behavior.

    If a thrown exception is not handled by an exception mapper, then no response filters are run.