[jboss-user] [JBoss Web Services] - Need Help -- Exception Mapper for http exception does not work resteasy

Ananta Amalkar do-not-reply at jboss.com
Sun Apr 11 21:09:45 EDT 2010


Ananta Amalkar [http://community.jboss.org/people/a_ananta] created the discussion

"Need Help -- Exception Mapper for http exception does not work resteasy"

To view the discussion, visit: http://community.jboss.org/message/536787#536787

--------------------------------------------------------------
Hi:

I'm implementing exception mapper so taht my application will throw approriate 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() {
       


        classes.add(MyRestEasyExceptionMapper.class);

        classes.add(UnauthorizedExceptionMapper.class);    
 
    }
    
    
    @Override
    public Set<Class<?>> getClasses() {
        return classes;
    }

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

2. UnauthorizedExceptionMapper.java

@Provider
public class UnauthorizedExceptionMapper implements ExceptionMapper<UnauthorizedException> {

    public Response toResponse(UnauthorizedException e)
       {
        return Response.status(Status.UNAUTHORIZED)
        .entity(e)
        .build();
        }

}

3. MyRestEasyExceptionMapper.java

@Provider
public class MyRestEasyExceptionMapper implements ExceptionMapper<RuntimeException>{


    private @Context Providers providers;


       @SuppressWarnings("unchecked")
    public Response toResponse(RuntimeException exception)
       {
          if (exception.getCause() == null)
          {
             return Response.serverError().build();
          }
          Class cause = exception.getCause().getClass();
          ExceptionMapper mapper = null;
          try{
           mapper = providers.getExceptionMapper(cause);    
          }catch(Exception e){
              System.out.println(e);
          }
         
          if (mapper == null)
          {
             return Response.serverError().build();
          }
          else
          {
             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();
b.validateAccess();

}


Class B{

method validateAccess() throws UnauthorizedException{
try{
// 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.

--------------------------------------------------------------

Reply to this message by going to Community
[http://community.jboss.org/message/536787#536787]

Start a new discussion in JBoss Web Services at Community
[http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2044]

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-user/attachments/20100411/57aeb345/attachment.html 


More information about the jboss-user mailing list