Cannot get error message from throwable object in method AsyncCallback.onFailure(Throwable throwable)

292 views
Skip to first unread message

Gurwinder

unread,
Jun 2, 2006, 2:22:10 PM6/2/06
to Google Web Toolkit
Hi,

I am using GWT RPC to invoke a method on my server, which throws an
exception (e.g new RuntimeException("my message")). This invokes
onFailure() method of my AsynCallback interface. I am trying to get get
the actual error message from the Throwable object in the onFailure()
method of my AsynCallback, but it always returns
"java.lang.RuntimeException/<some number>". I need to get the actual
error message, which is "my message" in this case.

//code for my asyn call back
AsyncCallback callback = new AsyncCallback() {
public void onSuccess(Object object) {
Window.alert("success");
}
public void onFailure(Throwable throwable) {
Window.alert(throwable.getMessage());
}
};

I tried to get the cause from the above throwable object, but it is
always null.

Could anyone please help? I will appreciate that.
Does anyone know when Google is coming up with the actual release of
GWT?

Regards,
Gurwinder

sbj

unread,
Jun 11, 2006, 3:09:17 PM6/11/06
to Google Web Toolkit
I am also having this problem (now using 1.0.21) - for me, both the
getMessage() and getCause() methods of the resulting Throwable return
null, even though the Throwable was created with both a message and a
cause on the server. This is deployed on Tomcat (not in hosted mode).

Does the serialization of Throwables does not include those fields?

Unless I am missing something, this would seem to make error reporting
difficult - It looks like to work around it I would need to recreate
the message and cause fields in my subclass of Throwable.

mP

unread,
Jun 11, 2006, 6:52:55 PM6/11/06
to Google Web Toolkit
The doco clearly states the limitations and behaviour of serialized
exceptions.

sbj

unread,
Jun 11, 2006, 11:18:53 PM6/11/06
to Google Web Toolkit
I'm afraid I disagree. If the documentation says anything about
dropping message and cause, it's not in any of these pages:

Developer Guide > Fundamentals > GWT Compiler > Language Support
Developer Guide > Fundamentals > GWT Compiler > Runtime Library Support
Developer Guide > Remote Procedure Calls > Handling Exceptions
Developer Guide > Remote Procedure Calls > Creating Services
Developer Guide > Remote Procedure Calls > Implementing Services
GWT Class Reference > com.google.gwt.user.client.rpc > AsyncCallback
JRE Emulation Library > java.lang > Throwable

All I found was "Throwable.getStackTrace() is not supported for web
mode". If I've overlooked something, I'm sorry, but I need a more
specific pointer to the documentation of which you speak.

Jason Essington

unread,
Jun 12, 2006, 10:35:45 AM6/12/06
to Google-We...@googlegroups.com
It isn't outlined in googles documentation, but here's how you do it.

To receive exceptions thrown by your RPC, you first need to define a
custom exception (extend Exception) and implement isSeralizable.

This class needs to live in the ...client package. Something like this:

class MyException extends Exception implements IsSeralizable
{
private String msg;
public MyException()
{
super(); // gotta have this one for GWT
}
public MyException(String msg)
{
super(msg);
this.msg = msg;
}
public String getMessage()
{
return this.msg;
}
}


your service interface must declare the exception to be thrown.

public interface MyService extends RemoteService
{
public String myMethod(String someParam) throws MyException;
}

The Async interface does not declare the exception in its method
public void myMethod(String somParam, AsyncCallback callback);

your server side implementation must also declare the exception,
obviously, since it implements the interface.

Then when you throw the exception from your service (server side) the
callback will receive your exception in the onFailure method rather
than the generic wrapper exception, and since it is serializable, you
can now call getMessage() to receive your message.

HTH

-jason

sbj

unread,
Jun 12, 2006, 11:37:21 AM6/12/06
to Google Web Toolkit
Ok, as I suspected. I guess it's hard to make Throwable serializable
because that would imply all of its subclasses are serializable as
well. I can also implement my own StackTrace support.

I wonder if it would be a good idea for GWT to provide an Exception
subclass with those features?

Gurwinder

unread,
Jun 13, 2006, 11:42:02 AM6/13/06
to Google Web Toolkit
Hi Jason,

Thank you so much for the help. I can now get error message from my
custom exception object.

Regards,
Gurwinder

Reply all
Reply to author
Forward
0 new messages