dev@jsr311.java.net

Re: JSR311: default EJBExceptionMapper in spec...

From: Bill Burke <bburke_at_redhat.com>
Date: Thu, 20 Aug 2009 13:23:46 -0400

Paul Sandoz wrote:
>
> On Aug 20, 2009, at 4:17 PM, Bill Burke wrote:
>
>>
>>
>> Paul Sandoz wrote:
>>> On Aug 19, 2009, at 7:33 PM, Bill Burke wrote:
>>>> I'm doing an EJB example in my book and came across the scenario of
>>>> EJBException. Maybe the spec should define a default
>>>> ExceptionMapper for it? Here's a simple one I did:
>>>>
>>>> @Provider
>>>> public class EJBExceptionMapper implements
>>>> ExceptionMapper<EJBException>
>>>> {
>>>> @Context
>>>> private Providers providers;
>>>>
>>>> public Response toResponse(EJBException exception)
>>>> {
>>>> if (exception.getCausedByException() == null)
>>>> {
>>>> return Response.serverError().build();
>>>> }
>>>> Class cause = exception.getCausedByException().getClass();
>>>> ExceptionMapper mapper = providers.getExceptionMapper(cause);
>>>> if (mapper == null)
>>>> {
>>>> return Response.serverError().build();
>>>> }
>>>> else
>>>> {
>>>> return mapper.toResponse(exception.getCausedByException());
>>>> }
>>>> }
>>>> }
>>> Note that the above solution assumes that by default there is an
>>> exception mapper for WebApplicationException. Only instances
>>> WebApplicationException that have no entity should be mapped:
>>
>> I am not following you. Don't see where WEbApplicationException comes
>> into play here.
>>
>>> https://jsr311.dev.java.net/nonav/releases/1.0/spec/spec3.html#x3-280003.3.4
>>> Thus one cannot always assume that providers.getExceptionMapper for
>>> a cause class that is assignable to WebApplication will return a
>>> non-null value.
>>
>> Ah, you're saying that if the cause is a WebApplicationException I
>> need to return the Response of the WAE if it exists.
>>
>
> Almost: if the cause is a WebApplicationException whose Response has an
> entity then the Response must be returned. Otherwise, a mapper can be
> obtained and if that mapper is null then the Response must be returned.
>
> I think this probably highlights some improvements we could make to the
> specification of providers.getExceptionMapper.
>

While on the topic of exceptions...

Users have suggested, and I have implemented an exception hiearchy.
NotFoundException, etc... That way they can write exception mappers for
different scenarios thrown by the JAX-RS runtime.

-- 
Bill Burke
JBoss, a division of Red Hat
http://bill.burkecentral.com