javagwtresty-gwt

restygwt - get original server error on client site


I have an application build on GWT + RestyGWT with Spring

I'm trying to make some user friendly exception handling on client site.

I have some method on server side that throws an exception:

@PostMapping(...)
@Transactional(...)
public long withdraw(@PathVariable(value = "amount) long amount) throws CustomException {
    if (amount < 0) {
        throw new CustomException("Amount is negative");
    }
    account.withdraw(amount);
    return account.balance;
}

It's called async from client side and handled there:

... new MethodCallback<...>() {
@Override
public void onFailure(Method method, Throwable throwable) {
    // here should be error handling
}

How can I get original error message and class ("Amount is negative" and CustomException)? All I could get from method and throwable variables were:


Solution

  • You cannot receive the same Exception in RestyGWT (this can be done using GWT RPC). But, you can handle exceptions uniformly in the server side (in jersey you can use an exception mapper in spring is called exception handler) and return an error response with a known JSON format. Then you can get this error response using the FailedResponseException (this is the exception you are receiving right now), this exception contains the response, so you can do MyKnownErrorResponse o = JSON.parse(failedResponseException.getResponse().getText()).