I'm writing GWT application where I need to send instance of java.lang.Throwable (with chain of it's causes and all stack traces respectively) using GWT RPC which uses standard Java serialization mechanism (as far as I'm concerned).
The problem is that when I pass following sample exception from client:
java.lang.RuntimeException (message=null, stacktrace A) caused by
java.io.IOException (message="Problems with io", stacktrace B) caused by
java.lang.IllegalStateException (message="Some error text", stacktrace C), cause=null
on the server I get the following:
java.lang.RuntimeException (message="java.io.IOException: Problems with io", stacktrace X) cause=this
where stacktrace X
is simply stack trace leading to place where this exception was deserialized on the server i.e. with no regards to original stack traces A, B or C. So stacktrace information is lost along with causes chain.
After reading superb article 7 Tips for Exception Handling in GWT it was found out that
the stack trace within an exception is transient, and so is lost from client to server (so if you need it on the server side, send it as a separate parameter)
After a bit of googling, I came to conclusion that topic of fully serializing/deserializing instances of java.lang.Throwable using standard Java serialization technique is not so popular. Actually I couldn't find neither libraries nor blogs with detailed description on how to achieve this.
Has anybody stucked upon and solved such problem before? Are there any suggested solutions to this problem?
Thanks in advance!
Even if is working I don't think it is wise to do so. In serialization we need to keep tap of what are attached to the object and make sure that when de-serialized it got all the right version of the classes it need otherwise failed. So, depend of the running environment, the exception stacktrace are different, platform, jvm version, additional libraries different.... So, think of the stacktrace as a snapshot in time for that environment and can not be reintroduce unless restored it the later time to the same environment. But in your requirement, it is intent to send from client to a server so, this will never work! The best thing to do is just to captured as string and save it as such:
And if you can not use StringWriter then try this:
Ok, found an elegant and simple solution to my problem: in GWT 2.5.1 there is a class designed specifically for those needs called
com.google.gwt.core.client.impl.SerializableThrowable
with following JavaDoc:So, code snippet solving my problem is the following:
If implemented in this way, it prints correct stack trace info along with correct causes.
NOTE In GWT 2.6.0 class
com.google.gwt.core.client.impl.SerializableThrowable
is deprecated in favor of com.google.gwt.core.shared.SerializableThrowable which differs from the first one only slightly and should work similarly.Try RemoteLoggingService to send the logs from client to server side. Here is a sample code:
web.xml:
GwtRemoteLogging.java:
gwt.xml (inherit logging and set properties):
client side code: