10
votes

In our (legacy) codebase, we're throwing WebApplicationExceptions in different ways. In an attempt to make some order in how we're handling exceptions - I wanted to create an ExceptionMapper for these WAEs (and others).

I realized, however, that Jersey's ExceptionMapper only maps WAE which weren't thrown with an entity.

For example:

throw new WebApplicationException(Response.status(500).build());

This exception is caught by the ExceptionMapper.

throw new WebApplicationException(Response.status(500).entity(WsResourceUtils.createWSResourceRestError(500, "bla")).build());

This exception is NOT caught by the ExceptionMapper.

Both are thrown from the same point in code.

This is my ExceptionMapper:

@Provider
public class GeneralExceptionMapper implements ExceptionMapper<Throwable> {

    private static final Logger logger = LoggerFactory.getLogger(GeneralExceptionMapper.class);

    @Override
    public Response toResponse(Throwable e) {
        logger.error("Caught a WAE", e);
...
}

Is it possible to create an ExceptionMapper which will catch WebApplicationExceptions even if their response is already built with an entity?

We're using Jersey 1.17.

Thanks.

1
2 years later, this is still an issue with Jersey 2.26 - ChrisO
Sorry to resurrect a thread that's nearly three years old, but RestEasy 3.0.11 also exhibits this. If I create my WAE without an entity, my mapper gets called. If I create it with the entity, my mapper never sees it. Perhaps this is defined in JAX-RS? - bmauter

1 Answers

0
votes

please consider looking at the code in GIST :

https://gist.github.com/jeorfevre/9fb2c447a01bcc724998

  1. Register a Mapper that of MagicException that contains Response
  2. Define Exception that contain response
  3. Throw this exception