Monday, September 13, 2010

Re: Unchecked exceptions from server to client

Your service probably extends RemoteServiceServlet, which is catching
all non declared exceptions. Simple solution is to create your own
RemoteServiceServlet implementation with overridden:
String processCall(String payload) throws SerializationException
Something like this:

try {
RPCRequest rpcRequest = RPC.decodeRequest(payload,
delegate.getClass(), this);
onAfterRequestDeserialized(rpcRequest);
return RPC.invokeAndEncodeResponse(delegate,
rpcRequest.getMethod(),
rpcRequest.getParameters(),
rpcRequest.getSerializationPolicy(),
rpcRequest.getFlags());
} catch (IncompatibleRemoteServiceException ex) {
log("An IncompatibleRemoteServiceException was thrown while
processing this call.",ex);
return RPC.encodeResponseForFailure(null, ex);
} catch (Exception ex) {
// RuntimeException
return RPC.encodeResponseForFailure(null, new
GwtClientException(ex.getCause().getMessage()));
}

try-catch is from GWT sources, 2nd catch is what u need to re-throw
exceptions to client.

On Sep 13, 9:24 pm, Kasper Hansen <kbhdk1...@gmail.com> wrote:
> Hi,
>
> I have this;
>
> public abstract class CreateException extends RuntimeException implements
> Serializable {
>
> }
>
> Notice that I extend from RuntimeException, making it an unchecked
> exception.
>
> Then I have these two;
>
> public class DuplicateEmailCreationException extends CreateException {
>
> }
>
> public class DuplicatePhoneCreationException extends CreateException {
>
> }
>
> My service is this one;
>
> @RemoteServiceRelativePath("AccountService")
> public interface AccountService extends RemoteService {
>
> public void createAccount(String phone, String email);
>
> }
>
> Notice I don't declare my CreateException using the "throws" keyword.
>
> Now I would expect my view to work. It's this one;
>
> ...
> accountServiceAsync.createAccount(tbPhone.getValue(), tbEmail.getValue(),
> new AsyncCallback<Void>() {
>
> public void onSuccess(Void result) {
>
> }
>
> public void onFailure(Throwable exception) {
>
> if ( exception instanceof DuplicatePhoneCreationException) {
>
> // handle it
>
> }
>
> else {
>
> if (exception instanceof DuplicateEmailCreationException) {
>
> // handle it
>
> }
> }
> }
>
> ...
>
> However, it does not work. But if I add the "throws CreateException" in the
> AccountService's createAccount method, it works fine.
>
> The whole point with unchecked exceptions, is that I don't have to define
> them everywhere. Is this not supported ? Or what am I doing wrong here ?
>
> Thanks,
>
> :-) Kasper

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment