7 Replies Latest reply on Apr 15, 2010 3:54 PM by Jozef Hartinger

    Issue with ExceptionMapper RestEasy with Seam

    Ananta Amalkar Newbie

      I'm implementing exception mapper so that application will throw appropriate http error code.

      here is my implementation:

      `1. MyRestApplication.java

      public class MyRestApplication extends Application{

          private Set<Object> singletons = new HashSet<Object>();
          private Set<Class<?>> classes = new HashSet<Class<?>>();

          public MyRestApplication() {



          public Set<Class<?>> getClasses() {
              return classes;

           * Return a set of rest easy implementation classes.
          public Set<Object> getSingletons() {
              return singletons;

      2. UnauthorizedExceptionMapper.java

      public class UnauthorizedExceptionMapper implements ExceptionMapper<UnauthorizedException> {

          public Response toResponse(UnauthorizedException e)
              return Response.status(Status.UNAUTHORIZED)


      3. MyRestEasyExceptionMapper.java

      public class MyRestEasyExceptionMapper implements ExceptionMapper<RuntimeException>{

          private @Context Providers providers;

          public Response toResponse(RuntimeException exception)
                if (exception.getCause() == null)
                   return Response.serverError().build();
                Class cause = exception.getCause().getClass();
                ExceptionMapper mapper = null;
                 mapper = providers.getExceptionMapper(cause);  
                }catch(Exception e){
                if (mapper == null)
                   return Response.serverError().build();
                   return mapper.toResponse(exception.getCause());



      I have registred the mappers using MyRestApplication. My method throws "org.jboss.resteasy.spi.UnauthorizedException", which is wrapped up by RuntimeException, like:

      Class A{

      method a() thorws RuntimeException{

      Class B b = new B();



      Class B{

      method validateAccess() throws UnauthorizedException{


      // validate access

      }Catch(RuntimeException e){

           throw new UnauthorizedException(e.getMessage(), e);




      It works well till it gets to find the MyRestEasyExceptionMapper. When control goes inside toResponse method of MyRestEasyExceptionMapper,

      I can see the Class cause = exception.getCause().getClass(); is UnauthorizedException, which is correct.

      So it means, the cause of the RuntimeException is unauthorisedexception, but then it can not find the mapper for this unauthorisedexception.

      mapper = providers.getExceptionMapper(cause);  this comes as null.

      I'm not able to figure out what is wrong here?

      For RuntimeException, it finds the correct mapper, but then from the mapper class of runtimeexception, it can not find the mapper for unauthorisedexception.

      Any help will be highly appreciated here.

      NOTE: I do see some deployment errors:

      23:25:44,671 ERROR [STDERR] 4094 [main] INFO org.jboss.resteasy.spi.ResteasyDeployment - Adding class @Provider common.service.ex
      ception.NotAcceptableExceptionHandler from Application javax.ws.rs.core.Application
      23:25:44,671 ERROR [STDERR] 4094 [main] INFO org.jboss.resteasy.spi.ResteasyDeployment - Adding class @Provider common.service.ex
      ception.MyRestEasyExceptionMapper from Application javax.ws.rs.core.Application

      23:25:44,687 ERROR [STDERR] 4110 [main] INFO org.jboss.resteasy.spi.ResteasyDeployment - Adding class @Provider common.service.ex
      ception.UnauthorizedExceptionMapper from Application javax.ws.rs.core.Application

      From web.xml