I have an exception that I throw from the server side that I wish to catch on the client side. The exception should be sent over REST using Jersey. This is what I have so far:
Define my Exception:
public class MyException extends Exception implements Serializable {
private static final long serialVersionUID = 1L;
public MyException () {
super();
}
public MyException (String msg) {
super(msg);
}
public MyException (String msg, Exception e) {
super(msg, e);
}
}
Define my exception in a class which implements the ExceptionMapper:
@Provider
public class TestMapper implements ExceptionMapper<MyException> {
@Override
public Response toResponse(MyExceptionexception){
return Response.status(Status.BAD_REQUEST).entity(exception.getMessage()).build();
}
}
This is my resource class, where the request from the server goes into:
@PUT
@Path("calculate")
public void calculateMethod(String id) throws MyException{
//Other code...
throw new MyException("custom error message string");
}
}
And finally, from the client side makes the REST call as follows:
public void calculateMethodClient(String id) throws MyException{
client().put("/test/", "calculate", id);
}
When this client method gets called, I have surrounded with Try/Catch to test:
public boolean testClient(){
try {
client.calculateMethodClient((String) id);
return true;
} catch (MyException) {
return false;
} catch (UniformInterfaceException e){
return false;
}
}
I have put a breakpoint in both catch blocks but it only ever hits the UniformInterfaceException
catch. This is not what I want as the server side can fail for a variety of reasons and I want to be able to define the error message in 'MyException' each time.
Could someone please explain how I can get this to work?