users@jersey.java.net

Re: [Jersey] Re: ExceptionMapper to Respect Exception Class Hierarchy?

From: Erik van Zijst <erik.van.zijst_at_gmail.com>
Date: Thu, 25 Sep 2008 16:24:14 +1000

Craig McClanahan wrote:
> Erik van Zijst wrote:
>> Folks,
>>
>> I guess I found the answer in the source:
>> ExceptionMapperFactory.find() locates all the exception mappers that
>> are capable of translating the exception and then returns the one that
>> is closest in terms of nodes in the class hierarchy.
>>
> This behavior is also consistent with the JAX-RS specification
> requirements (see Section 4.4 of the latest spec at [1]), so it should
> be consistent across JAX-RS implementations as well.

Excellent.

Erik


>> cheers,
>> Erik
> Craig
>
> [1] http://jcp.org/en/jsr/detal?id=311
>
>>
>>
>> Erik van Zijst wrote:
>>> 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
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
>