GWT Throwing Exception for Customer

Can anyone show to exclude client to client in GWT.

in my serviceasync interface i do this also in my service interface

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

in my serverimpl;

I do this to create an exception

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

my exception is in the form:

 public class AlreadyActivatedError extends Exception implements IsSerializable { public AlreadyActivatedError() { super(); } } 
+7
source share
4 answers

I don’t know why the hilal answer was accepted, since it is completely false.

In order for exceptions to fall into the client browser, you must throw a checked exception , which is Serializable and defined in the service interfaces.

If your service implementation throws a RuntimeException , the web client will receive 500 general error messages as follows:

  [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) 
+5
source

Just to make everything clear: you can throw both checked (those that extend Exception ) and unchecked (those that extend RuntimeException ) exceptions from server to client β€” as long as the exception is serializable . However, it is recommended that you throw checked exceptions as they are

constitute unacceptable conditions in areas beyond the direct control of the program (invalid user input, database problems, network failures, missing files).

On the contrary, excluded exceptions

represent defects in the program (errors) - often invalid arguments are passed to a non-private method.

A source

As stated in the documentation , the following conditions must be met to send an exception to the client:

  • It should extend Exception (note that RuntimeException does this).
  • Must be serializable . In short: Serializable implementation, have no-args constructor and have all fields serializable.
  • In your *Service interface, you need to add a throws declaration to a method that may throw an exception. Please note that you do not need to add a throws declaration to the *Async interface.

After you configure this setting, you can handle the exception in the onFailure method of AsyncCallback .

Some code to show all parts together, based on examples from the manual on the GWT website :

DelistedException.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; } } 

StockPriceService.java

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

StockPriceServiceAsync.java

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

Client side

 AsyncCallback<StockPrice[]> callback = new AsyncCallback<StockPrice[]>() { public void onFailure(Throwable caught) { if (caught instanceof DelistedException) { // Do something with it } else { // Probably some unchecked exception, // show some generic error message } public void onSuccess(StockPrice[] result) { // Success! } }; stockPriceService.getPrices(symbols, callback); 
+1
source

GWT can only handle thrown exceptions (which means it is sent from the server to the client) that are not serialized, that extend from RuntimeException . I do not know what AlreadyActivatedError . If this is not a RuntimeException, it cannot be sent to the client (browser)

0
source

Perhaps also note that if your specific exception does not have a standard constructor, a general 500 error message will also be displayed, because it is impossible to exclude an exception from the exception. Unfortunately, the gwt compiler and the dev-mode plugin will not always point to this problem explicitly. (Since I'm here anyway)

So, in combination with what vinnyjames said (the exception must be Serializable and be defined in the RPC service interface), I found that handling exceptions in the gwt client code is not too complicated.

0
source

All Articles