db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject svn commit: r453368 - /db/ojb/trunk/src/java/org/apache/ojb/broker/util/ExceptionHelper.java
Date Thu, 05 Oct 2006 20:32:50 GMT
Author: arminw
Date: Thu Oct  5 13:32:50 2006
New Revision: 453368

URL: http://svn.apache.org/viewvc?view=rev&rev=453368
Log:
add new methods

Modified:
    db/ojb/trunk/src/java/org/apache/ojb/broker/util/ExceptionHelper.java

Modified: db/ojb/trunk/src/java/org/apache/ojb/broker/util/ExceptionHelper.java
URL: http://svn.apache.org/viewvc/db/ojb/trunk/src/java/org/apache/ojb/broker/util/ExceptionHelper.java?view=diff&rev=453368&r1=453367&r2=453368
==============================================================================
--- db/ojb/trunk/src/java/org/apache/ojb/broker/util/ExceptionHelper.java (original)
+++ db/ojb/trunk/src/java/org/apache/ojb/broker/util/ExceptionHelper.java Thu Oct  5 13:32:50
2006
@@ -15,11 +15,12 @@
  * limitations under the License.
  */
 
-import java.sql.SQLException;
 import java.sql.BatchUpdateException;
+import java.sql.SQLException;
 import java.sql.SQLWarning;
 
 import org.apache.commons.lang.SystemUtils;
+import org.apache.commons.lang.ArrayUtils;
 import org.apache.commons.lang.exception.ExceptionUtils;
 import org.apache.ojb.broker.KeyConstraintViolatedException;
 import org.apache.ojb.broker.PersistenceBrokerSQLException;
@@ -112,6 +113,85 @@
                                                                   String sql, ClassDescriptor
cld,
                                                                   ValueContainer[] values,
Object obj, Logger logger)
     {
+        StringBuffer msg = new StringBuffer();
+        String eol = SystemUtils.LINE_SEPARATOR;
+        // message string for PB exception
+        String shortMsg = generateMessage(message, ex, sql, cld, values, obj, false);
+        if(ex != null)
+        {
+            // add causing stack trace
+            Throwable rootCause = ExceptionUtils.getRootCause(ex);
+            if(rootCause == null) rootCause = ex;
+            msg.append(eol).append("The root stack trace is --> ");
+            String rootStack = ExceptionUtils.getStackTrace(rootCause);
+            msg.append(eol).append(rootStack);
+        }
+        msg.append(SystemUtils.LINE_SEPARATOR).append("**");
+
+        // log error message
+        if(logger != null) logger.error(msg.toString());
+
+        // throw a specific type of runtime exception for a key constraint.
+        String stateCode = ex != null ? ex.getSQLState() : null;
+        if("23000".equals(stateCode) || "23505".equals(stateCode))
+        {
+            throw new KeyConstraintViolatedException(shortMsg, ex);
+        }
+        else
+        {
+            throw new PersistenceBrokerSQLException(shortMsg, ex);
+        }
+    }
+
+    /**
+     * Generates a detailed error message based on the specified exception.
+     *
+     * @param message The error message to use, if <em>null</em> a standard message
is used.
+     * @param ex The exception to convert (mandatory).
+     * @param sql The used sql-statement or <em>null</em>.
+     * @return A detailed error message.
+     */
+    public static String generateMessage(String message, SQLException ex, String sql)
+    {
+        return generateMessage(message, ex, sql, null, null, null);
+    }
+
+    /**
+     * Generates a detailed error message based on the specified exception.
+     *
+     * @param message The error message to use, if <em>null</em> a standard message
is used.
+     * @param ex The exception to convert (mandatory).
+     * @param sql The used sql-statement or <em>null</em>.
+     * @param cld The {@link org.apache.ojb.broker.metadata.ClassDescriptor} of the target
object or <em>null</em>.
+     * @param values The values set in prepared statement or <em>null</em>.
+     * @param obj The target object or <em>null</em>.
+     * @return A detailed error message.
+     */
+    public static String generateMessage(String message, SQLException ex, String sql,
+                                         ClassDescriptor cld,
+                                         ValueContainer[] values,
+                                         Object obj)
+    {
+        return generateMessage(message, ex, sql, cld, values, obj, true);
+    }
+
+    /**
+     * Generates a detailed error message based on the specified exception.
+     *
+     * @param message The error message to use, if <em>null</em> a standard message
is used.
+     * @param ex The exception to convert (mandatory).
+     * @param sql The used sql-statement or <em>null</em>.
+     * @param cld The {@link org.apache.ojb.broker.metadata.ClassDescriptor} of the target
object or <em>null</em>.
+     * @param values The values set in prepared statement or <em>null</em>.
+     * @param obj The target object or <em>null</em>.
+     * @param withRootStack If <em>true</em> the root stack trace will be added
too.
+     * @return A detailed error message.
+     */
+    public static String generateMessage(String message, SQLException ex, String sql,
+                                         ClassDescriptor cld,
+                                         ValueContainer[] values,
+                                         Object obj, boolean withRootStack)
+    {
         /*
         X/OPEN codes within class 23:
         23000	INTEGRITY CONSTRAINT VIOLATION
@@ -134,9 +214,11 @@
             }
             else
             {
-                msg.append("* BatchUpdateException during execution of sql-statement:");
+                msg.append("* BatchUpdateException during execution of batch-statement:");
             }
-            msg.append(eol).append("Batch update count is '").append(tmp.getUpdateCounts()).append("'");
+            msg.append(eol).append("Batch update count array before exception is '")
+                    .append(ArrayUtils.toString(tmp.getUpdateCounts()))
+                    .append("'");
         }
         else if(ex instanceof SQLWarning)
         {
@@ -163,14 +245,14 @@
 
         if(sql != null)
         {
-            msg.append(eol).append("sql statement was '").append(sql).append("'");
+            msg.append(eol).append("sql statement: ").append(sql);
         }
-        String stateCode = null;
+        String stateCode;
         if(ex != null)
         {
-            msg.append(eol).append("Exception message is [").append(ex.getMessage()).append("]");
-            msg.append(eol).append("Vendor error code [").append(ex.getErrorCode()).append("]");
-            msg.append(eol).append("SQL state code [");
+            msg.append(eol).append("Exception message: ").append(ex.getMessage());
+            msg.append(eol).append("Vendor error code: ").append(ex.getErrorCode());
+            msg.append(eol).append("SQL state code: ");
 
             stateCode = ex.getSQLState();
             if("23000".equalsIgnoreCase(stateCode)) msg.append(stateCode).append("=INTEGRITY
CONSTRAINT VIOLATION");
@@ -180,7 +262,6 @@
             else if("23505".equalsIgnoreCase(stateCode)) msg.append(stateCode).append("=UNIQUE
VIOLATION");
             else if("23514".equalsIgnoreCase(stateCode)) msg.append(stateCode).append("=CHECK
VIOLATION");
             else msg.append(stateCode);
-            msg.append("]");
         }
 
         if(cld != null)
@@ -234,10 +315,7 @@
             }
         }
 
-        // message string for PB exception
-        String shortMsg = msg.toString();
-
-        if(ex != null)
+        if(ex != null && withRootStack)
         {
             // add causing stack trace
             Throwable rootCause = ExceptionUtils.getRootCause(ex);
@@ -248,17 +326,17 @@
         }
         msg.append(SystemUtils.LINE_SEPARATOR).append("**");
 
-        // log error message
-        if(logger != null) logger.error(msg.toString());
+        return msg.toString();
+    }
 
-        // throw a specific type of runtime exception for a key constraint.
-        if("23000".equals(stateCode) || "23505".equals(stateCode))
-        {
-            throw new KeyConstraintViolatedException(shortMsg, ex);
-        }
-        else
-        {
-            throw new PersistenceBrokerSQLException(shortMsg, ex);
-        }
+    /**
+     * Returns a stack trace string.
+     *
+     * @param message The message used by the internal created exception.
+     * @return Stack trace of current caller.
+     */
+    public static String buildExceptionStack(String message)
+    {
+        return ExceptionUtils.getStackTrace(new Exception(message));
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message