Reputation: 56
I have some strange problem with dropwizard exception handling. I wrote custom exception mapper very similar to this http://gary-rowe.com/agilestack/2012/10/23/how-to-implement-a-runtimeexceptionmapper-for-dropwizard/ . For status 400 my code is
if (webAppException.getResponse().getStatus() == 400) {
return Response
.status(Response.Status.BAD_REQUEST)
.entity("Request sent to server is bad.")
.build();
}
In code when i use throw new WebApplicationException(400)
it works great, mapper catches exception and return response with a message but when i modify code to look like this
String msg=webAppException.getResponse().getEntity().toString();
return Response
.status(Response.Status.BAD_REQUEST)
.entity("Request sent to server is bad."+msg)
.build();
and in code use something like this
throw new WebApplicationException(Response.status(400).entity("hello!").build())
it returns just "hello!". Mapper does not catch this exception at all. It only catch when i provide just status code. I removed all dropwizard mappers using instructions from this link http://thoughtspark.org/2013/02/25/dropwizard-and-jersey-exceptionmappers/ but it still does not catch this exception. It is not because of above modification in mapper because it does not call "toResponse" method at all. So, my question is why it behave like this. I could leave like this it is "good enough", but i want to know why it wont work and it would be better if it works with mapper so i can handle exceptions easier.
Thanks in advance
Upvotes: 2
Views: 2004
Reputation: 11529
By debugging we found that this behaviour is done in org.glassfish.jersey.server.ServerRuntime
(mapException
method).
This method checks if (throwable instanceof WebApplicationException)
and then if (waeResponse.hasEntity())
, and in that case it just returns the waeResponse
without calling any exception mapper.
So, when you throw a WebApplicationException with a response that contains an entity, it won't reach your exception mapper.
Upvotes: 2