ravis
ravis

Reputation: 41

RestEasy ExceptionMapper not catching the exceptions

I'm throwing an exception MyCustomException from my application. (EJB Layer)

I've an exception mapper in web service layer which looks like following -

package net.webservices;

import javax.ws.rs.core.Response; 
import javax.ws.rs.ext.Provider; 
import net.common.MyCustomException;

@Provider 
public class EJBExceptionMapper implements 
ExceptionMapper<net.common.MyCustomException>  {

  public Response toResponse(MyCustomException exception) {
    return Response.status(Response.Status.BAD_REQUEST).build();    
}   

  }

I've registered my mapper in web.xml of the web service layer as following -

 <context-param>
    <param-name>resteasy.providers</param-name>
    <param-value>net.webservices.EJBExceptionMapper</param-value>        
</context-param>

The EJBExceptionMapper is not catching the MyCustomException. But instead its being caught by the catch block of the web service implementation.

What could be the problem?

Note: I don't want to register my ExceptionMapper manually using getProviderFactory().addExceptionMapper()

Upvotes: 4

Views: 4093

Answers (2)

Kranthi
Kranthi

Reputation: 117

You need to throw exception (of type MyCustomException ) in the catch block and add a "Throws MyCustomException" to the method signature

Upvotes: 0

Artefacto
Artefacto

Reputation: 97815

I don't know why your solution doesn't work (but I've never used RESTeasy, only Jersey). In any case, it would probably be simpler to extend WebApplicationException. That way, you don't have to register a provider:

public class MyCustomException extends WebApplicationException {
    public MyCustomException() {
        super(Response.status(Response.Status.BAD_REQUEST).build());
    }
}

Upvotes: 2

Related Questions