JFace ErrorDialog: how do I show something in the details?

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

I want to show an exception message in the main area and a call stack in the details area. However, both of these options 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 to change it?

+4
source share
3 answers

In the standard JFace ErrorDialog, the only way to show the full trace of the exception stack (the same as printStackTrace ()) is to build each line of the stack trace as one status. Finally, set these statuses as children of MultiStatus.

Here is an example utility method that I use in our RCP applications:

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

You can wrap the exception with a new one containing stacktrace as a 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); } }); } 
+3
source

It looks like you are mixing 2nd and 3rd parameters in openError. The third parameter is the message that will be displayed. Since you give stacktrace, it shows it.

Once you get this fix, you can look at using MultiStatus.

+1
source

Source: https://habr.com/ru/post/1309665/


All Articles