Errai JAX-RS throwing and catching exceptions (howto?)
yagerhard May 23, 2013 8:36 PMI'm having some difficulties working out how to throw a userException from a JAX-WS serverImpl class and then catch it nicely in the client class.
The service definition is like this:
---------------------------------------------
@Path("blah")
public interface BlahService {
@POST
@Consumes("application/json")
@Produces("application/json")
public MyResult fooOperation(MyRequest myRequest) throws myException;
---------------------------------------------
The ServiceImpl class would look like this:
---------------------------------------------
public classBlahServiceImpl implements BlahService{
@Override
public MyResult fooOperation(MyRequest myRequest) throws myException{
If (happy days){
// Do something
return new MyResult();
} else {
throw new MyException("myException");
}
}
---------------------------------------------
The client would look like this
---------------------------------------------
private void letsDoIt(){
MyRequest myRequest = new MyRequest();
try {
blahService.call(blahCallback).fooOperation(myRequest);
System.out.println("Success");
} catch (MyException e) {
System.out.println("Error [" + e.getMessage() + "]");
}
}
final RemoteCallback<MyResult> blahCallback = new RemoteCallback<MyResult>() {
@Override
public void callback(MyResult myResult) {
// Do something here with myResult
System.out.println("Doing something useful with the result");
}
};
-------------------------------------------
When I call "letsDoIt" from the client, The sever gets invoked correctly and the result is returned and everything is great.
If however, I throw an exception in the Impl class, the "catch (MyException e)" code in "letsDoIt" does not get called. As far as "letsDoIt" is concerned, the operation was a success (the console even prints out "success". However, when the method completes, the underlying Erria error handling notices that an exception has occurred and proceeds to produce stack traces etc. I've tried including an errorHandler callback like this: "blahService.call(blahCallback, myErrorCallback).fooOperation(myRequest);", but this seems to aimed at more cataclysmic communication errors rather than simple application exceptions.
So how should I be catching "MyException" in the client?
Many thanks,
MarkB