0

I use the following Exceptionmapper to map WebApplicationExceptions in my jaxrs rest api to responses.

@Provider
public class ErrorHandler implements ExceptionMapper<WebApplicationException> {
    @Override
    public Response toResponse(WebApplicationException e) {
        int status = e.getResponse().getStatus();

        JsonObject errorResponse = Json.createObjectBuilder()
                .add("status", status)
                .add("message", e.getMessage())
                .build();

        return Response.status(status)
                .entity(errorResponse)
                .type(MediaType.APPLICATION_JSON)
                .build();
    }
}

This works fine and it does exactly what it should do, but when I throw custom errors, for example throw new NotFoundException("custom message"); the stacktrace shows up in my server log. Can anyone explain this? Or does anyone know of a solution?

TL;DR;

For some reason when I throw WebApplicationExceptions from my jax-rs code, my ExceptionMapper handles the error but still throws it so it shows up in the server log.

Any solutions?

Goos van den Bekerom
  • 1,475
  • 3
  • 20
  • 32
  • It may sound pretty obvious, but maybe some part of the code has already logged and rethrown the exception? – Georg Muehlenberg Mar 23 '18 at 16:12
  • Thanks for your comment. This could very well be how java-ee does this, the exceptions are thrown inside injected EJB beans managed by the application server. but if this is the case I would like to know for sure. The weird thing is though, when jaxrs throws a NotFoundException itself, it won't show up in the log. – Goos van den Bekerom Mar 23 '18 at 16:15
  • My first thought is: Can't you see it in the Stacktrace who logs this? My second thought: You should be able to configure the logging to disable logging of certain exception types with Jaxrs. Does [this question](https://stackoverflow.com/questions/39656572/disable-logging-of-exception-in-bean) help you with it? – Georg Muehlenberg Mar 23 '18 at 16:19
  • @GeorgMuehlenberg Thanks! Your comments made me think about the priority of my jaxrs providers, googling for that found me the solution. I've posted it as an answer. – Goos van den Bekerom Mar 23 '18 at 16:34
  • 1
    Sometimes, a comment can be helpful in a mediate way ;-) – Georg Muehlenberg Mar 23 '18 at 16:47

1 Answers1

1

I've found the origin of this problem and managed to solve it.

From the JAX-RS spec

When choosing an exception mapping provider to map an exception, an implementation MUST use the provider whose generic type is the nearest superclass of the exception.

In my ExceptionMapper I used the WebApplicationException, so every error would be mapped. The problem is that WebApplicationException is not the nearest superclass of (e.g.) NotFoundException. There is a ClientErrorException superclass inbetween. When I changed my mapper to that class the problem was solved.

Since I only want to map client errors to Json responses this solution works fine for me.

Goos van den Bekerom
  • 1,475
  • 3
  • 20
  • 32