From 8213b81bb81d462f08ce9c429f3445f36b35fa16 Mon Sep 17 00:00:00 2001
From: neil_a_wilson <neil_a_wilson@localhost>
Date: Fri, 20 Apr 2007 23:28:00 +0000
Subject: [PATCH] Update the code to provide a convenience method for generating more user-friendly error messages from exceptions. For exceptions generated within the project, we'll just use the user-provided message and also display the unique message ID. For null pointer exceptions, we'll display the fact that it was a null pointer exception and the source file and line number on which it occurred. For invocation target exceptions, we'll handle the wrapped exception. For all other types of exceptions, we'll display the name of the exception class and the message, or if there is no message the source file and line number.
---
opends/src/server/org/opends/server/util/StaticUtils.java | 84 ++++++++++++++++++++++++++++++++++++++++-
1 files changed, 81 insertions(+), 3 deletions(-)
diff --git a/opends/src/server/org/opends/server/util/StaticUtils.java b/opends/src/server/org/opends/server/util/StaticUtils.java
index 4bde017..b45bbe7 100644
--- a/opends/src/server/org/opends/server/util/StaticUtils.java
+++ b/opends/src/server/org/opends/server/util/StaticUtils.java
@@ -51,6 +51,7 @@
import org.opends.server.types.AttributeValue;
import org.opends.server.types.DN;
import org.opends.server.types.Entry;
+import org.opends.server.types.IdentifiedException;
import org.opends.server.types.ObjectClass;
import org.opends.server.types.RDN;
@@ -72,9 +73,6 @@
*/
public final class StaticUtils
{
-
-
-
/**
* Private constructor to prevent instantiation.
*/
@@ -1431,6 +1429,86 @@
}
}
+
+
+ /**
+ * Retrieves the best human-readable message for the provided exception. For
+ * exceptions defined in the OpenDS project, it will attempt to use the
+ * message (combining it with the message ID if available). For some
+ * exceptions that use encapsulation (e.g., InvocationTargetException), it
+ * will be unwrapped and the cause will be treated. For all others, the
+ *
+ *
+ * @param t The {@code Throwable} object for which to retrieve the message.
+ *
+ * @return The human-readable message generated for the provided exception.
+ */
+ public static String getExceptionMessage(Throwable t)
+ {
+ if (t instanceof IdentifiedException)
+ {
+ IdentifiedException ie = (IdentifiedException) t;
+
+ StringBuilder message = new StringBuilder();
+ message.append(ie.getMessage());
+ message.append(" (id=");
+ message.append(ie.getMessageID());
+ message.append(")");
+ return message.toString();
+ }
+ else if (t instanceof NullPointerException)
+ {
+ StackTraceElement[] stackElements = t.getStackTrace();
+
+ StringBuilder message = new StringBuilder();
+ message.append("NullPointerException(");
+ message.append(stackElements[0].getFileName());
+ message.append(":");
+ message.append(stackElements[0].getLineNumber());
+ message.append(")");
+ return message.toString();
+ }
+ else if ((t instanceof InvocationTargetException) &&
+ (t.getCause() != null))
+ {
+ return getExceptionMessage(t.getCause());
+ }
+ else
+ {
+ StringBuilder message = new StringBuilder();
+
+ String className = t.getClass().getName();
+ int periodPos = className.lastIndexOf('.');
+ if (periodPos > 0)
+ {
+ message.append(className.substring(periodPos+1));
+ }
+ else
+ {
+ message.append(className);
+ }
+
+ message.append("(");
+ if (t.getMessage() == null)
+ {
+ StackTraceElement[] stackElements = t.getStackTrace();
+ message.append(stackElements[0].getFileName());
+ message.append(":");
+ message.append(stackElements[0].getLineNumber());
+ }
+ else
+ {
+ message.append(t.getMessage());
+ }
+
+ message.append(")");
+
+ return message.toString();
+ }
+ }
+
+
+
/**
* Retrieves a stack trace from the provided exception as a single-line
* string.
--
Gitblit v1.10.0