15

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

user291701
  • 34,331
  • 68
  • 176
  • 277
  • 1
    It would be make the question better if you can mention which version of GWT you are using and what logging technique you are following on client side. – appbootup Dec 02 '12 at 04:38
  • 1
    Which stack trace are you talking about? compile time or run time? If it is run time, just use any logger, or surround your code with try catch block and transfer the exception stack trace to whatever string you want – Abhijith Nagaraja Dec 02 '12 at 06:11
  • Hi, run time exception - I'm using GWT 2.5. Yeah the thing is, I want to convert a full stack trace to a string, and then maybe display it in a Label. But how can we get an exception instance stack trace as a string? – user291701 Dec 02 '12 at 23:44

4 Answers4

13

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";
}
Riley Lark
  • 19,952
  • 14
  • 75
  • 122
9

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;
}
Florent Bayle
  • 9,983
  • 3
  • 31
  • 42
3

I would not recommend trying to display error stack trace in a GUI label.

1) They are not readable after GWT Obfuscation. They just look like bunch of tab aligned characters over new lines.

2) They are not in I18N format.

3) The correct way is the just show user a well formed error "Message" . exception.getMessage() will give you a single line of non-obf information which should provide the necessary UX interaction to user.

4) If you are looking for well formed exception stacktrace helpful for debugging ( not for user ) you should use GWT's well documented logging feature with web mode exceptions -

a) https://developers.google.com/web-toolkit/doc/latest/DevGuideLogging

b) Also read on http://code.google.com/p/google-web-toolkit/wiki/WebModeExceptions

appbootup
  • 9,486
  • 3
  • 30
  • 64
1

Use com.google.gwt.logging.impl.StackTracePrintStream

Throwable t = ...;
StringBuilder message = new StringBuilder();
StackTracePrintStream ps = new StackTracePrintStream(message);
t.printStackTrace(ps);
ps.flush();
sidon
  • 1,344
  • 1
  • 15
  • 30