Search code examples
javagwterrai

Handling Service call errors with ErraiCallback


What is the requirement in a Service implementation so that the ErrorCallback will be able to print the error message on the UI like.

I tried injecting a ErrorCallback in the Service call code, and when I print the Message object its null.

What should be the Service implementation look like, should I put throws SomeException on the implementation method? Or?

myService.call(new RemoteCallback<String>() {
    @Override
    public void callback(String response) {
        Multimap<String, String> state =  ArrayListMultimap.create();
        state.put("hash", response);
        submit.go(state);
    }
}, new ErrorCallback() {
    @Override
    public boolean error(Message message, Throwable throwable) {
        throwable.printStackTrace();
        Window.alert("Error: " + message);
        return false;
    }
}).createmy(my);

Solution

  • Your code should work as stated in the question. The exception thrown by the remote service should be delivered to you in the throwable parameter to your ErrorCallback.error() method.

    You only need to put a throws clause on the remote interface if you want to throw a checked exception. Unchecked exceptions should work as you've done it. In fact, there is a disadvantage to declaring checked exceptions on remote interfaces: you will have to surround your RPC calls with a useless try/catch block, like this:

    @Remote public interface MyService {
      Result dangerousOperation() throws MyCheckedException;
    }
    

    and the calling code:

    try {
      myService.call(new RemoteCallback<Result>() {
        @Override
        public void callback(final Result result) {
          Window.alert("Yay, got a result: " + result);
        }
      },
      new BusErrorCallback() {
        @Override
        public boolean error(Message message, Throwable throwable) {
          Window.alert("Got an exception from the remote service: " + throwable);
          return false;
        }
      }).dangerousOperation();
    }
    catch (MyCheckedException e) {
      throw new AssertionError(); // can't happen: caller stub won't throw this
    }
    

    So you're probably better off with the unchecked exception.