GWT向客户端抛出异常

时间:2011-01-28 06:47:09

标签: gwt exception-handling throw

有人可以展示在GWT中向客户端抛出异常。

在我的serviceasync界面中我也在我的服务界面中执行此操作

void ActivateUserAccount(String ActivationCode,AsyncCallback <Boolean> Callback) throws AlreadyActivatedError;

在我的serverimpl中;

我这样做是为了抛出异常

public Boolean ActivateUserAccount(String ActivationCode) throws AlreadyActivatedError
    {
....
throw new AlreadyActivatedError();
}

我的例外情况是:

public class AlreadyActivatedError extends Exception implements IsSerializable
{
    public AlreadyActivatedError()
    {
        super();
    }
}

4 个答案:

答案 0 :(得分:5)

不确定为什么hilal的答案被接受,因为它完全是假的。

为了让例外到达客户端的浏览器,您必须抛出Serializable RuntimeException并在服务接口中定义。

如果您的服务实现引发 [WARN] Exception while dispatching incoming RPC call com.google.gwt.user.server.rpc.UnexpectedException: Service method 'public abstract my.module.shared.Result my.module.client.service.Service.doSomething() throws my.module.shared.exception.MyCheckedException' threw an unexpected exception: my.module.shared.exception.AuthzRuntimeException: some authz message at com.google.gwt.user.server.rpc.RPC.encodeResponseForFailure(RPC.java:389) at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:579) at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:208) at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:248) at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62) at javax.servlet.http.HttpServlet.service(HttpServlet.java:637) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487) at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362) at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216) at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181) at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:729) at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405) at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) at org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:49) at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) at org.mortbay.jetty.Server.handle(Server.java:324) at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505) at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:843) at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:647) at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211) at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380) at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395) at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488) Caused by: my.module.shared.exception.AuthzRuntimeException: some authz message at my.module.shared.Authz.doSomeCheck(Authz.java:101) at my.module.server.ServiceImpl.doSomething(ServiceImpl.java:283) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:561) ... 22 more [ERROR] 500 - POST /module/userService (127.0.0.1) 57 bytes Request headers Host: 127.0.0.1:8888 User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-US,en;q=0.5 Accept-Encoding: gzip, deflate Referer: http://127.0.0.1:8888/foo.html?gwt.codesvr=127.0.0.1:9997 Cookie: worknet=7k8g41ulbl674dp865t180tji5 Connection: keep-alive Cache-Control: no-cache X-GWT-Permutation: HostedMode X-GWT-Module-Base: http://127.0.0.1:8888/module/ Content-Type: text/x-gwt-rpc; charset=utf-8 Content-Length: 1076 Pragma: no-cache Response headers Content-Type: text/plain (RequestCallbackAdapter.java:209) 2013-10-18 13:16:56,966 [WARN ] Fail "" com.google.gwt.user.client.rpc.StatusCodeException: 500 The call failed on the server; see server log for details at com.google.gwt.user.client.rpc.impl.RequestCallbackAdapter.onResponseReceived(RequestCallbackAdapter.java:209) at com.google.gwt.http.client.Request.fireOnResponseReceived(Request.java:258) at com.google.gwt.http.client.RequestBuilder$1.onReadyStateChange(RequestBuilder.java:412) at sun.reflect.GeneratedMethodAccessor40.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103) at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71) at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172) at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:338) at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:219) at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136) at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:571) at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:279) at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91) at com.google.gwt.core.client.impl.Impl.apply(Impl.java) at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:242) at sun.reflect.GeneratedMethodAccessor37.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103) at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71) at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172) at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:293) at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:547) at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364) at java.lang.Thread.run(Unknown Source) ,则Web客户端将收到如下通用500错误消息:

{{1}}

答案 1 :(得分:4)

只是为了使一切清楚:只要异常为{{{},就可以将已检查(扩展Exception)和未检查(扩展RuntimeException)异常从服务器抛出到客户端。 3}}。但是建议抛出已检查的异常,因为它们

  

表示程序直接控制范围之外的区域中的无效条件(无效的用户输入,数据库问题,网络中断,缺少文件)。

相反,未经检查的例外

  

表示程序中的缺陷(错误) - 通常是传递给非私有方法的无效参数。

serializable

作为Source,必须满足以下条件才能将异常发送到客户端:

  • 必须延长Exception(请注意,RuntimeException会这样做。)
  • 必须是documentation states。简而言之:实现Serializable,使用no-args构造函数并使所有字段都可序列化。
  • *Service界面中,您需要向可抛出异常的方法添加throws声明。请注意,您不需要将throws声明添加到*Async界面。

完成设置后,您就可以在onFailure的{​​{1}}方法中处理异常。

根据serializable

中的示例,一些代码可以显示所有部分

DelistedException.java

AsyncCallback

StockPriceService.java

public class DelistedException extends Exception implements Serializable {

  private String symbol;

  // Note the no-args constructor
  // It can be protected so that only subclasses could use it
  // (because if they want to be serializable too, they'll need
  // a no-args constructor that calls the superclasses' no-args constructor...)
  protected DelistedException() {
  }

  public DelistedException(String symbol) {
    this.symbol = symbol;
  }

  public String getSymbol() {
    return this.symbol;
  }
}

StockPriceServiceAsync.java

@RemoteServiceRelativePath("stockPrices")
public interface StockPriceService extends RemoteService {
  StockPrice[] getPrices(String[] symbols) throws DelistedException;
}

在客户端

public interface StockPriceServiceAsync {
  void getPrices(String[] symbols, AsyncCallback<StockPrice[]> callback);
}

答案 2 :(得分:0)

GWT只能处理从unchecked exceptions延伸的未序列化的RuntimeException(这意味着从服务器发送到客户端)。我不知道AlreadyActivatedError是什么。如果它不是RuntimeException,则无法将其发送到客户端(浏览器)

答案 3 :(得分:0)

也许还要注意,如果您的特定Exception没有标准构造函数,则还会显示泛型500错误消息,因为无法反序列化异常。遗憾的是,gwt编译器和开发模式插件并不总是明确地指出这个问题。(这就是我到这里的方式)

因此,结合vinnyjames所说的(异常必须是Serializable并在(RPC)服务的接口中定义),我发现在gwt客户端代码中处理异常并不太复杂。