I tried
public void onFailure(Throwable caught) {
Throwable cause = caught.getCause();
String causeStr = (cause==null) ? "" : ", "+cause.getMessage();
errorLabel.setText(SERVER_ERROR + ": " + caught.getMessage() + causeStr);
But cause is always null and caught.getMessage()
always equals the very generic 500 The call failed on the server; see server log for details
. I want to throw IllegalArgumentExceptions from the server and be able to show it on the client:
throw new IllegalArgumentException("Email address is invalid.");
You could also override the
RequestFactoryServlet
and pass it a custom exception handler::In web.xml ::
I also found you can send back a Google UmbrellaException, but you have to instantiate it a little funny because it only takes Sets in the constructor:
Server
Client
Console
I liked Zied's and Fred's answers the best as they are the simplest and most transparent. However, no need to use UncaughtExceptionHandler or create SystemExceptions, so it can be even simpler. Just capture exceptions as normal, re-wrap, and throw. No need to litter the server Interface with exceptions (just yours). Any serious errors like OutOfMemoryError will be handled by GWT as normal. Also simpler instantiation than my other answer. GWT already has pass/fail handlers with
onSuccess/onFailure
so no need to re-check for a failure withinonSuccess
with a special return value. However, the only way to reachonFailure
is with an Exception, so even though a boolean might be sufficient, an Exception is required to indicate an error to the client handler.MyException
Server
Your Exception needs to be Serializable to travel through the cables.
In addition, the best practice says: You should have two exception kinds:
This way you will write System exceptions in the logs and send back business exceptions to the user
You can use
com.google.gwt.core.client.GWT.UncaughtExceptionHandler
to catch the exception on the server, and then throw your own exception thatimplements
Serializable
, andis defined in a source folder that is acccessible to (and compiled for) the client.