ExceptionMapper for WebApplicationExceptions thrown with entity?

ε祈祈猫儿з 提交于 2019-12-21 04:03:00

问题


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:


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


来源:https://stackoverflow.com/questions/29414041/exceptionmapper-for-webapplicationexceptions-thrown-with-entity

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!