Skip to content

Commit

Permalink
[eclipse] Display exception stack trace into the error dialogs.
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Sep 22, 2017
1 parent 89bdd8a commit d4a372e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 32 deletions.
Expand Up @@ -21,20 +21,26 @@

package io.sarl.eclipse;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.google.common.base.Strings;
import com.google.common.base.Throwables;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.osgi.internal.debug.Debug;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.service.prefs.BackingStoreException;

Expand Down Expand Up @@ -139,9 +145,8 @@ public ImageDescriptor getImageDescriptor(String imagePath) {
* @param cause - the cause of the problem.
* @return the status.
*/
@SuppressWarnings("static-method")
public IStatus createStatus(int severity, String message, Throwable cause) {
return new Status(severity, PLUGIN_ID, message, cause);
return createStatus(severity, 0, message, cause);
}

/** Create a status.
Expand All @@ -152,9 +157,35 @@ public IStatus createStatus(int severity, String message, Throwable cause) {
* @param cause - the cause of the problem.
* @return the status.
*/
@SuppressWarnings("static-method")
public IStatus createStatus(int severity, int code, String message, Throwable cause) {
return new Status(severity, PLUGIN_ID, code, message, cause);
String msg = message;
if (Strings.isNullOrEmpty(msg)) {
msg = cause.getLocalizedMessage();
if (Strings.isNullOrEmpty(msg)) {
msg = cause.getMessage();
}
if (Strings.isNullOrEmpty(msg)) {
msg = cause.getClass().getSimpleName();
}
}
if (cause != null) {
final List<IStatus> childStatuses = new ArrayList<>();
final StackTraceElement[] stackTraces = cause.getStackTrace();
for (final StackTraceElement stackTrace: stackTraces) {
final IStatus status = createStatus(severity, stackTrace.toString());
childStatuses.add(status);
}

final IStatus[] children = new IStatus[childStatuses.size()];
if (!childStatuses.isEmpty()) {
childStatuses.toArray(children);
}
return new MultiStatus(PLUGIN_ID,
code,
children,
msg, cause);
}
return new Status(severity, PLUGIN_ID, code, msg, cause);
}

/** Create a status.
Expand All @@ -164,14 +195,7 @@ public IStatus createStatus(int severity, int code, String message, Throwable ca
* @return the status.
*/
public IStatus createStatus(int severity, Throwable cause) {
String message = cause.getLocalizedMessage();
if (Strings.isNullOrEmpty(message)) {
message = cause.getMessage();
}
if (Strings.isNullOrEmpty(message)) {
message = cause.getClass().getSimpleName();
}
return createStatus(severity, message, cause);
return createStatus(severity, 0, null, cause);
}

/** Create a status.
Expand All @@ -182,14 +206,7 @@ public IStatus createStatus(int severity, Throwable cause) {
* @return the status.
*/
public IStatus createStatus(int severity, int code, Throwable cause) {
String message = cause.getLocalizedMessage();
if (Strings.isNullOrEmpty(message)) {
message = cause.getMessage();
}
if (Strings.isNullOrEmpty(message)) {
message = cause.getClass().getSimpleName();
}
return createStatus(severity, code, message, cause);
return createStatus(severity, code, null, cause);
}

/** Create a status.
Expand All @@ -198,9 +215,8 @@ public IStatus createStatus(int severity, int code, Throwable cause) {
* @param message - the message associated to the status.
* @return the status.
*/
@SuppressWarnings("static-method")
public IStatus createStatus(int severity, String message) {
return new Status(severity, PLUGIN_ID, message);
return createStatus(severity, 0, message);
}

/** Create a status.
Expand All @@ -210,9 +226,8 @@ public IStatus createStatus(int severity, String message) {
* @param message - the message associated to the status.
* @return the status.
*/
@SuppressWarnings("static-method")
public IStatus createStatus(int severity, int code, String message) {
return new Status(severity, PLUGIN_ID, code, message, null);
return createStatus(severity, code, message, null);
}

/** Create a ok status.
Expand Down Expand Up @@ -310,6 +325,7 @@ public void logDebugMessage(String message, Throwable cause) {
* Logs an internal error with the specified throwable.
*
* @param exception the exception to be logged
* @see #openError(Shell, String, String, Throwable)
*/
public void log(Throwable exception) {
if (exception instanceof CoreException) {
Expand Down Expand Up @@ -346,4 +362,25 @@ public void savePreferences() {
}
}

/**
* Display an error dialog and log the error.
*
* @param shell the parent container.
* @param title the title of the dialog box.
* @param message the message to display into the dialog box.
* @param exception the exception to be logged.
* @since 0.6
* @see #log(Throwable)
*/
public void openError(Shell shell, String title, String message, Throwable exception) {
final Throwable ex = (exception != null) ? Throwables.getRootCause(exception) : null;
if (ex != null) {
log(ex);
final IStatus status = createStatus(IStatus.ERROR, message, ex);
ErrorDialog.openError(shell, title, message, status);
} else {
MessageDialog.openError(shell, title, message);
}
}

}
Expand Up @@ -54,7 +54,6 @@
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
Expand All @@ -80,6 +79,7 @@
import org.eclipse.xtext.ui.resource.IStorage2UriMapper;
import org.eclipse.xtext.util.Pair;

import io.sarl.eclipse.SARLEclipsePlugin;
import io.sarl.eclipse.launching.config.ILaunchConfigurationAccessor;
import io.sarl.eclipse.launching.config.ILaunchConfigurationConfigurator;
import io.sarl.eclipse.util.Jdt2Ecore;
Expand Down Expand Up @@ -199,7 +199,8 @@ protected ILaunchConfiguration createConfiguration(String projectName, String fu
try {
return this.configurator.newConfiguration(projectName, fullyQualifiedNameOfAgent);
} catch (CoreException exception) {
MessageDialog.openError(getShell(), Messages.SARLLaunchShortcut_0, exception.getStatus().getMessage());
SARLEclipsePlugin.getDefault().openError(getShell(),
Messages.SARLLaunchShortcut_0, exception.getStatus().getMessage(), exception);
return null;
}
}
Expand All @@ -216,7 +217,8 @@ private void searchAndLaunch(String mode, Object... scope) {
final List<AgentDescription> agents = findAgents(scope, PlatformUI.getWorkbench().getProgressService());
AgentDescription agent = null;
if (agents.isEmpty()) {
MessageDialog.openError(getShell(), Messages.SARLLaunchShortcut_0, Messages.SARLLaunchShortcut_2);
SARLEclipsePlugin.getDefault().openError(getShell(), Messages.SARLLaunchShortcut_0, Messages.SARLLaunchShortcut_2,
null);
} else if (agents.size() > 1) {
agent = chooseType(agents);
} else {
Expand All @@ -226,13 +228,15 @@ private void searchAndLaunch(String mode, Object... scope) {
try {
launch(agent.projectName, agent.agentName, mode);
} catch (CoreException e) {
MessageDialog.openError(getShell(), Messages.SARLLaunchShortcut_0, e.getMessage());
SARLEclipsePlugin.getDefault().openError(getShell(), Messages.SARLLaunchShortcut_0,
e.getStatus().getMessage(), e);
}
}
} catch (InterruptedException exception) {
//
} catch (InvocationTargetException exception) {
MessageDialog.openError(getShell(), Messages.SARLLaunchShortcut_0, exception.getLocalizedMessage());
SARLEclipsePlugin.getDefault().openError(getShell(), Messages.SARLLaunchShortcut_0, null,
exception);
}
}

Expand Down
Expand Up @@ -73,7 +73,6 @@
import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogFieldGroup;
import org.eclipse.jdt.ui.wizards.NewTypeWizardPage;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableContext;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.IStructuredSelection;
Expand Down Expand Up @@ -722,8 +721,8 @@ protected void execute(IProgressMonitor monitor)
return 0;
} catch (InvocationTargetException e) {
final Throwable realException = e.getTargetException();
SARLEclipsePlugin.getDefault().log(realException);
MessageDialog.openError(getShell(), getTitle(), realException.getMessage());
SARLEclipsePlugin.getDefault().openError(getShell(), getTitle(),
realException.getMessage(), realException);
}
return size[0];
}
Expand Down

0 comments on commit d4a372e

Please sign in to comment.