JFace ErrorDialog:我如何在细节部分显示内容?

时间:2021-08-09 19:11:52

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

ErrorDialog。openError为对话框标题、消息和状态(有消息本身)提供参数。

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?

我该怎么换呢?

3 个解决方案

#1


8  

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.

默认情况下,JFace ErrorDialog只能显示完整的异常堆栈跟踪(与printStackTrace()生成的相同)是将堆栈跟踪的每一行构建为一个状态。最后将这些状态设置为多状态的childen。

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

下面是我在RCP应用程序中使用的实用方法示例:

/**
 * 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);
}

#2


3  

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

可以用包含stacktrace作为消息的新内容包装异常。

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);
        }
    });
}

#3


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.

看起来你把第2和第3个参数混合在了openError上。第三个参数是要显示的消息。因为你给了stacktrace它显示了它。

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

一旦你得到了修复,你可能想要使用多状态。

#1


8  

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.

默认情况下,JFace ErrorDialog只能显示完整的异常堆栈跟踪(与printStackTrace()生成的相同)是将堆栈跟踪的每一行构建为一个状态。最后将这些状态设置为多状态的childen。

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

下面是我在RCP应用程序中使用的实用方法示例:

/**
 * 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);
}

#2


3  

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

可以用包含stacktrace作为消息的新内容包装异常。

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);
        }
    });
}

#3


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.

看起来你把第2和第3个参数混合在了openError上。第三个参数是要显示的消息。因为你给了stacktrace它显示了它。

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

一旦你得到了修复,你可能想要使用多状态。