使用ExceptionMapper记录CLIENT_ERROR

时间:2018-09-10 14:52:22

标签: java exception-handling jax-rs dropwizard exceptionmapper

我正在使用使用Dropwizard的应用程序,该应用程序具有ExceptionMapper的以下实现:https://github.com/dropwizard/dropwizard/blob/master/dropwizard-jersey/src/main/java/io/dropwizard/jersey/errors/LoggingExceptionMapper.java

此实现的问题是,即使它捕获了4 **和5 **错误,也只记录了5 **错误。

我需要实现ExceptionMapper,以便根本不使用LoggingExceptionMapper,而我的CustomExceptionMapper会记录CLIENT_ERRORs和SERVER_ERRORs。

我想知道我的应用程序如何知道它需要使用CustomExceptionMapper而不是Dropwizard类?

是否还可以将CLIENT_ERROR添加到if条件,以注销所有错误?

@Override
public Response toResponse(E exception) {
    // If we're dealing with a web exception, we can service certain types of request (like
    // redirection or server errors) better and also propagate properties of the inner response.
    if (exception instanceof WebApplicationException) {
        final Response response = ((WebApplicationException) exception).getResponse();
        Response.Status.Family family = response.getStatusInfo().getFamily();
        if (family.equals(Response.Status.Family.REDIRECTION)) {
            return response;
        }
        if (family.equals(Response.Status.Family.SERVER_ERROR) || family.equals(Response.Status.Family.CLIENT_ERROR) {
            logException(exception);
        }

        return Response.fromResponse(response)
                .type(MediaType.APPLICATION_JSON_TYPE)
                .entity(new ErrorMessage(response.getStatus(), exception.getLocalizedMessage()))
                .build();
    }

或者会有更好的方法吗?

0 个答案:

没有答案
相关问题