devshorts
devshorts

Reputation: 8872

Catch json processing exception with dropwizard?

I have a dropwizard application with a global exception handler registered that implements ExceptionMapper<Throwable>. If I throw any runtime exception from within a resource the mapper gets hit.

However, in a test I am posting JSON to to a resource with a missing type discriminator for a jackson polymorphic type. Jersey is returning a 400, and I can see that a JsonProcessingException is being thrown when stepping through the code, but the global mapper isn't getting hit.

I tried to see if there were any other exception mappers registered and to try and unregister them, by checking the environment.jersey().getResourceConfig().getSingletons() set and there were no other exception mappers registered.

I've also tried creating a specific handler for just that exception but no dice. And even then that's kind of weird if I already have a global handler to catch all Throwable.

Has anyone encountered this?

--

EDIT:

looks like the dropwizard exception mapper does get registered and I somehow need to find out when in the lifecycle that happens and unregister it

Upvotes: 2

Views: 1751

Answers (1)

Natan
Natan

Reputation: 2858

Yes, you do need to override the exception mapper for JsonProcessingException (JsonProcessingExceptionMapper).

looks like the dropwizard exception mapper does get registered and I somehow need to find out when in the lifecycle that happens and unregister it

You can find out how to override an ExceptionMapper on this answer.

Upvotes: 3

Related Questions