Hi,

I have implemented a "trappable command", that will show a popup in
case of an uncaught exception during its execution. It seems that a
stack trace is not available using the normal Java methods - at least
not when run in a browser. Any suggestions?

Here's the code.


/**
 * Implement the execute method for a command that will have a
reasonable
 * way to handle thrown exceptions.
 */
public abstract class TrappableCommand implements Command {

    /**
     * Execute this command, report any exception in a popup panel.
     */
    public void execute() {
        execute(this);
    }

    /**
     * Execute the provided command, report any exception in a popup
panel.
     * @param cmd The command to execute.
     */
    public static void execute(final TrappableCommand cmd) {
        try {
            cmd.executeWithCatch();
        } catch (final Throwable t) {
            final PopupPanel panel = new PopupPanel(true);
            panel.setWidget(makeWidget(t));
            panel.center();
        }
    }

    /**
     * Make a normal command trappable.
     *
     * @param cmd The command to make trappable.
     * @return A trappable wrapper for cmd.
     */
    public static Command makeTrappable(final Command cmd) {
        return new TrappableCommand() {

            @Override
            protected void executeWithCatch() {
                cmd.execute();
            }};
    }

    private static Widget makeWidget(final Throwable t) {
        final VerticalPanel host = new VerticalPanel();
        host.add(LabelFactory.makeNewLabelBiggest("Exception
caught"));
        addThrowable(t, host);
        return host;    }

    private static void addThrowable(final Throwable t, final
VerticalPanel host) {
        host.add(new Label("Class: " + t.getClass().getName()));
        host.add(new Label("Message:"));
        addStringWithLinebreaks(host, t.getMessage());
        host.add(new Label("Stacktrace:"));
        addWithLinebreaks(host, t.getStackTrace());
        if (t.getCause() != null) {
            host.add(LabelFactory.makeNewLabelBig("Caused by"));
            addThrowable(t.getCause(), host);
        } else {
            host.add(new Label("No cause"));
        }
    }

    private static void addWithLinebreaks(VerticalPanel host,
StackTraceElement[] stackTrace) {
        if (stackTrace.length == 0) {
            host.add(new Label("No stacktrace"));
        } else {
            for (StackTraceElement item : stackTrace) {
                host.add(new Label(item.toString()));
            }
        }
    }

    private static void addStringWithLinebreaks(VerticalPanel host,
String s) {
        String strings[] = s.split("\n");
        for (String str : strings) {
            host.add(new Label(str));
        }
    }

    /**
     * Implement to add behavior.
     */
    protected abstract void executeWithCatch() ;

}




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

Reply via email to