简体   繁体   中英

Dropwizard Exception Mapping

I have created two ExceptionMappers:

GenericExceptionMapper implements ExceptionMapper<Throwable>

and

ValidationExceptionMapper implements ExceptionMapper<javax.validation.ValidationException>

The GenericExceptionMapper checks if the Exception is of a specific type and if it is some mapping will be done. In all other cases it will be mapped to a 500.

The ValdiationExceptionMapper will map to a 400 with a specific response.

Now i'm observing several strange things. If i omit the ValidationExceptionMapper the GenericExceptionMapper is not called. Furthermore if i cut of the database connection non of the ExceptionMappers will be triggered. In both cases the response is a 500 html format. What i actually want is that every exception should be catched by the mapper.

I'm using Dropwizard 1.0.5 with the setting registerDefaultExceptionMappers: false .

First of all, I don't think setting @Singleton is good idea for a Resource.

On the other hand: This is the correct way to customize the response given for a particular exception by implementing ExtendedExceptionMapper :

environment.jersey().register(new ExtendedExceptionMapper<WebApplicationException>() {
@Override
public Response toResponse(WebApplicationException exception) {
    return Response.status(Response.Status.NOT_FOUND).build();
}

@Override
public boolean isMappable(WebApplicationException e) {
    return Throwables.getRootCause(e).getClass() == MustacheNotFoundException.class;
}});

Dropwizard docs, Templates errors section

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM