问题
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.");
回答1:
You can use com.google.gwt.core.client.GWT.UncaughtExceptionHandler to catch the exception on the server, and then throw your own exception that
implements
Serializable, andis defined in a source folder that is acccessible to (and compiled for) the client.
回答2:
Your Exception needs to be Serializable to travel through the cables.
In addition, the best practice says: You should have two exception kinds:
- SystemException: that is a fatal exception, the user can't recover (this should not be serializable since you will give the user a feedback of the type "an error occured on the server, please contact the administrator"
- BusinessException: which is due to a misuse of your ammplication by the user (ex: UnauthorizedException, BadMailException or more generally InvalidvalueException)
This way you will write System exceptions in the logs and send back business exceptions to the user
回答3:
You could also override the RequestFactoryServlet and pass it a custom exception handler::
public class CustomRequestFactoryServlet extends RequestFactoryServlet {
private static class ApplicationExceptionLogger implements ExceptionHandler {
private final Logger log = LoggerFactory.getLogger(ApplicationExceptionLogger.class);
@Override
public ServerFailure createServerFailure(Throwable throwable) {
log.error("Server Error", throwable);
return new ServerFailure(throwable.getMessage(), throwable.getClass().getName(), throwable.getStackTrace().toString(), true);
}
}
public CustomRequestFactoryServlet() {
super(new ApplicationExceptionLogger());
}
}
In web.xml ::
<servlet>
<servlet-name>requestFactoryServlet</servlet-name>
<servlet-class>com.myvdm.server.CustomRequestFactoryServlet</servlet-class>
</servlet>
回答4:
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
public String getUserId () throws Exception {
Set<Throwable> s = new HashSet<Throwable>(Arrays.asList(new IllegalArgumentException("Hidey hidey ho!")));
if (true) throw new com.google.gwt.event.shared.UmbrellaException(s);
Client
public void onFailure(Throwable caught) {
log.severe("fetchUserName(), Could not fetch username: " + caught.getMessage());
Console
Mon Oct 14 12:05:28 EDT 2013 com.example.client.Login
SEVERE: fetchUserName(), Could not fetch username: Exception caught: Hidey hidey ho!
回答5:
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 within onSuccess with a special return value. However, the only way to reach onFailure 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
package com.example.shared;
import java.io.Serializable;
public class MyException extends Exception implements Serializable {
private static final long serialVersionUID = 1104312904865934899L;
public MyException() {}
public MyException (String s) {
super(s);
}
}
Server
public void cancelSend() throws MyException {
throw new MyException("Because I said so");
来源:https://stackoverflow.com/questions/19324026/how-do-i-get-the-server-side-exception-message-with-gwt