JFace ErrorDialog: how do I show something in the details portion?
Asked Answered
D

3

5

ErrorDialog.openError takes arguments for dialog title, message, and status (which has a message itself).

I want to show the exception's message in the main area, and the call stack in the details area. However, both of these variations show the call stack in the main area:

void showException(Throwable e) {
    Status status = 
        new Status(IStatus.ERROR, "SCS Admin", e.getLocalizedMessage(), e);
    e.printStackTrace;
    ErrorDialog.openError(getShell(), null, Util.getStackTrace(e), status);
}

void showException(Throwable e) {
    Status status = 
        new Status(IStatus.ERROR, "SCS Admin", Util.getStackTrace(e), e);
    e.printStackTrace;
    ErrorDialog.openError(getShell(), null, e.getLocalizedMessage(), status);
}

How can I switch it around?

Deferred answered 13/5, 2010 at 13:5 Comment(0)
M
9

In default JFace ErrorDialog only way to show full exception stack trace (same as produced by printStackTrace()) is to build each row of stack trace as one status. And finally set these statuses as childen of MultiStatus.

Here's example of utility method I use in our RCP apps:

/**
 * Shows JFace ErrorDialog but improved by constructing full stack trace in
 * detail area.
 */
public static void errorDialogWithStackTrace(String msg, Throwable t) {

    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    t.printStackTrace(pw);

    final String trace = sw.toString(); // stack trace as a string

    // Temp holder of child statuses
    List<Status> childStatuses = new ArrayList<>();

    // Split output by OS-independend new-line
    for (String line : trace.split(System.getProperty("line.separator"))) {
        // build & add status
        childStatuses.add(new Status(IStatus.ERROR, Activator.PLUGIN_ID, line));
    }

    MultiStatus ms = new MultiStatus(Activator.PLUGIN_ID, IStatus.ERROR,
            childStatuses.toArray(new Status[] {}), // convert to array of statuses
            t.getLocalizedMessage(), t);

    ErrorDialog.openError(null, PxConstants.DIALOG_TITLE, msg, ms);
}
Modesta answered 22/2, 2012 at 22:23 Comment(0)
M
3

You could wrap the exception with a new that contains the stacktrace as message.

public void showException(final Exception ex) {
    Display.getDefault().syncExec(new Runnable() {
        @Override
        public void run() {
            StringWriter sw = new StringWriter();
            ex.printStackTrace(new PrintWriter(sw));
            IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, ex.getMessage(), new Exception(sw.toString()));
            ErrorDialog.openError(Display.getDefault().getActiveShell(), "Error", null, status);
        }
    });
}
Magical answered 31/10, 2013 at 9:55 Comment(0)
B
1

Looks like you're mixing up the 2nd and 3rd parameter on the openError. The 3rd parameter is the message to be shown. Since you're giving the stacktrace it shows it.

Once you get that fixed you might want to look at using a MultiStatus.

Bagdad answered 5/11, 2010 at 21:9 Comment(1)
This is an comment, not answer to original question.Modesta

© 2022 - 2024 — McMap. All rights reserved.