added checks to makes sure no other exception is thrown within the dialog creation...
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / model / MessagingUtils.java
index 959e49d92df4715109cbf5ca0e329b6139c91b6e..e9033c3925ca875a46b6840bd22b1ebcd6482f8a 100644 (file)
@@ -29,6 +29,7 @@ import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
 public class MessagingUtils {
     public final static String UNEXPECTED_ERROR_MESSAGE = "This is an unexpected error.";
     public final static String CONTACT_MESSAGE = System.getProperty("line.separator") +  "Please contact EDIT Support (EditSupport@bgbm.org) with the error trace below (click on the 'Details' button).";
+    public final static String DEFAULT_MESSAGE = "Error thrown but no associated message";
 
     /**
      * Gets the Log4J logger for a given class
@@ -197,19 +198,30 @@ public class MessagingUtils {
 
     public static String getStackTraceAndContextInfo(Throwable t, List<String> contextInfo)  {
         StringBuffer stackTraceAndContextInfo = new StringBuffer();
+        Throwable throwable = t;
 
         for(String infoItem : contextInfo) {
             stackTraceAndContextInfo.append(infoItem + System.getProperty("line.separator"));
         }
 
         StringWriter sw = new StringWriter();
-        t.printStackTrace(new PrintWriter(sw));
+
+        if(throwable == null) {
+            throwable = getDefaultThrowable();
+        }
+        throwable.printStackTrace(new PrintWriter(sw));
 
         stackTraceAndContextInfo.append(sw.toString());
 
         return stackTraceAndContextInfo.toString();
     }
 
+    private static Throwable getDefaultThrowable() {
+        return new Throwable("Error thrown but no associated exception");
+    }
+
+
+
     /**
      * Displays a {@link eu.etaxonomy.taxeditor.model.CdmErrorDialog}.
      *
@@ -263,7 +275,7 @@ public class MessagingUtils {
             final Throwable t,
             boolean addContactMesg) {
 
-
+        Throwable throwable = t;
         StringBuffer sbStackTrace = new StringBuffer();
 
         // We need to build a MultiStatus object since the simple
@@ -277,16 +289,20 @@ public class MessagingUtils {
             childStatuses.add(new Status(IStatus.ERROR, pluginId, infoItem));
         }
 
+        if(throwable == null) {
+            throwable = getDefaultThrowable();
+        }
+
         // add main execption
-        for (StackTraceElement ste : t.getStackTrace()) {
+        for (StackTraceElement ste : throwable.getStackTrace()) {
             childStatuses.add(new Status(IStatus.ERROR, pluginId, "at " + ste.toString()));
         }
 
         // add cause
-        if(t.getCause() != null) {
+        if(throwable.getCause() != null) {
             childStatuses.add(new Status(IStatus.ERROR, pluginId, ""));
-            childStatuses.add(new Status(IStatus.ERROR, pluginId, "Caused by : " + t.getCause().toString()));
-            for (StackTraceElement ste : t.getCause().getStackTrace()) {
+            childStatuses.add(new Status(IStatus.ERROR, pluginId, "Caused by : " + throwable.getCause().toString()));
+            for (StackTraceElement ste : throwable.getCause().getStackTrace()) {
                 // build & add status
                 childStatuses.add(new Status(IStatus.ERROR, pluginId, "at " + ste.toString()));
             }
@@ -295,7 +311,7 @@ public class MessagingUtils {
         String finalMessage = message;
 
         if(finalMessage == null || finalMessage.isEmpty()) {
-            finalMessage = "";
+            finalMessage = DEFAULT_MESSAGE;
         }
 
         if(addContactMesg) {
@@ -306,37 +322,10 @@ public class MessagingUtils {
         MultiStatus ms = new MultiStatus(pluginId,
                 IStatus.ERROR,
                 childStatuses.toArray(new Status[] {}),
-                t.toString(),
-                t);
+                throwable.toString(),
+                throwable);
 
-        errorDialog(title, source, t, contextInfo, finalMessage, ms);
-    }
-
-    /**
-     * Displays a {@link eu.etaxonomy.taxeditor.model.CdmErrorDialog}.
-     *
-     * @param title
-     *            a {@link java.lang.String} object.
-     * @param source
-     *            a {@link java.lang.Object} object.
-     * @param status
-     *            a {@link org.eclipse.core.runtime.IStatus} object.
-     */
-    private static void errorDialog(final String title,
-            final Object source,
-            final String message,
-            final IStatus status) {
-
-        Display.getDefault().asyncExec(new Runnable() {
-
-            @Override
-            public void run() {
-                CdmErrorDialog ced = new CdmErrorDialog(AbstractUtility.getShell(), title, message, status);
-                ced.open();
-                Class<? extends Object> clazz = source != null ? source.getClass() : this.getClass();
-                error(clazz, status);
-            }
-        });
+        errorDialog(title, source, throwable, contextInfo, finalMessage, ms);
     }
 
     /**