dev@jsr311.java.net

Re: JSR311: default EJBExceptionMapper in spec...

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 20 Aug 2009 15:27:39 +0200

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:

   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.

Paul.
>
> --
> Bill Burke
> JBoss, a division of Red Hat
> http://bill.burkecentral.com
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe_at_jsr311.dev.java.net
> For additional commands, e-mail: dev-help_at_jsr311.dev.java.net
>