users@jersey.java.net

ExceptionMapper to Respect Exception Class Hierarchy?

From: Erik van Zijst <erik.van.zijst_at_gmail.com>
Date: Thu, 25 Sep 2008 12:49:42 +1000

Hi folks,

I'm refactoring to using ExceptionMapper to get rid of the
exception-to-return-code translations in my services, but I'm unsure if
I'll still be able to tanslate exception subclasses into specific errors.

For example, I want FooException (which extends RuntimeException) to be
mapped to 409 (Conflict), while I want any other RuntimeExceptions that
pop out, to be translated into a 400 (Bad Request) and finally, I want
all other (checked) exceptions to generate a normal 500, but still
control the contents of the body of the response by using a custom mapper.

In my existing code, I just organize the catch clauses so they first
catch FooException, then RuntimeException, followed by Exception.

However, when using ExceptionMappers, I'm afraid that when my service
throws a FooException, it may get caught by the RuntimeExceptionMapper
and translated to 400 instead of 409.

Is there a way to ensure every exception gets caught by the appropriate
mapper?

cheers,
Erik