Is it possible to print a stack trace to a string in GWT? The usual methods of using the classes in java.io won't work I think, because the java.io package is not available clientside (and Writer, PrintWriter, etc are in that package)
Thank you
Analyze external stack tracesFrom the main menu, select Code | Analyze Stack Trace or Thread Dump. In the Analyze Stack Trace dialog that opens, paste the external stack trace or thread dump into the Put a stacktrace here: text area. Click OK. The stack trace is displayed in the Run tool window.
The short answer is that all you have to do to print the stack trace of an exception using Java and Log4J (or the Apache Commons Logging project) is this: log. error("Your description here", exception);
Using StringWriter and PrintWriter in the catch block, and the purpose behind it is to print the given output in the form of a string. Now print the stack trace using the printStackTrace() method of the exception and after that write it in the writer. And finally, convert it into a string using the toString() method.
I'm not sure if StackTraceElement is emulated, but if it is you can run something like
for (StackTraceElement element : exception.getStackTrace()) {
string += element + "\n";
}
Here is the method I'm using to retrieve a full stack trace as a String
in GWT :
private static String getMessage (Throwable throwable) {
String ret="";
while (throwable!=null) {
if (throwable instanceof com.google.gwt.event.shared.UmbrellaException){
for (Throwable thr2 :((com.google.gwt.event.shared.UmbrellaException)throwable).getCauses()){
if (ret != "")
ret += "\nCaused by: ";
ret += thr2.toString();
ret += "\n at "+getMessage(thr2);
}
} else if (throwable instanceof com.google.web.bindery.event.shared.UmbrellaException){
for (Throwable thr2 :((com.google.web.bindery.event.shared.UmbrellaException)throwable).getCauses()){
if (ret != "")
ret += "\nCaused by: ";
ret += thr2.toString();
ret += "\n at "+getMessage(thr2);
}
} else {
if (ret != "")
ret += "\nCaused by: ";
ret += throwable.toString();
for (StackTraceElement sTE : throwable.getStackTrace())
ret += "\n at "+sTE;
}
throwable = throwable.getCause();
}
return ret;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With