db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/broker/util ExceptionHelper.java
Date Thu, 27 Oct 2005 17:34:54 GMT
arminw      2005/10/27 10:34:54

  Modified:    src/java/org/apache/ojb/broker/util Tag: OJB_1_0_RELEASE
                        ExceptionHelper.java
  Log:
  add new and convenience methods
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.2.2.2   +88 -23    db-ojb/src/java/org/apache/ojb/broker/util/ExceptionHelper.java
  
  Index: ExceptionHelper.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/ExceptionHelper.java,v
  retrieving revision 1.2.2.1
  retrieving revision 1.2.2.2
  diff -u -r1.2.2.1 -r1.2.2.2
  --- ExceptionHelper.java	22 Oct 2005 12:21:41 -0000	1.2.2.1
  +++ ExceptionHelper.java	27 Oct 2005 17:34:54 -0000	1.2.2.2
  @@ -40,6 +40,23 @@
        * Method which support the conversion of {@link java.sql.SQLException} to
        * OJB's runtime exception (with additional message details).
        *
  +     * @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 logger The {@link org.apache.ojb.broker.util.logging.Logger} to log an detailed
message
  +     * to the specified {@link org.apache.ojb.broker.util.logging.Logger} or <em>null</em>
to skip logging message.
  +     * @return A new created {@link org.apache.ojb.broker.PersistenceBrokerSQLException}
based on the specified
  +     *         arguments.
  +     */
  +    public static PersistenceBrokerSQLException generateException(String message, SQLException
ex,  String sql, Logger logger)
  +    {
  +        return generateException(message, ex, sql, null, null, logger, null);
  +    }
  +
  +    /**
  +     * Method which support the conversion of {@link java.sql.SQLException} to
  +     * OJB's runtime exception (with additional message details).
  +     *
        * @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>.
  @@ -70,6 +87,26 @@
        */
       public static PersistenceBrokerSQLException generateException(SQLException ex,  String
sql, ClassDescriptor cld, ValueContainer[] values, Logger logger, Object obj)
       {
  +        return generateException(null, ex, sql, cld, values, logger, obj);
  +    }
  +
  +    /**
  +     * Method which support the conversion of {@link java.sql.SQLException} to
  +     * OJB's runtime exception (with additional message details).
  +     *
  +     * @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 logger The {@link org.apache.ojb.broker.util.logging.Logger} to log an detailed
message
  +     * to the specified {@link org.apache.ojb.broker.util.logging.Logger} or <em>null</em>
to skip logging message.
  +     * @param obj The target object or <em>null</em>.
  +     * @return A new created {@link org.apache.ojb.broker.PersistenceBrokerSQLException}
based on the specified
  +     *         arguments.
  +     */
  +    public static PersistenceBrokerSQLException generateException(String message, SQLException
ex,  String sql, ClassDescriptor cld, ValueContainer[] values, Logger logger, Object obj)
  +    {
           /*
           X/OPEN codes within class 23:
           23000	INTEGRITY CONSTRAINT VIOLATION
  @@ -86,35 +123,60 @@
           if(ex instanceof BatchUpdateException)
           {
               BatchUpdateException tmp = (BatchUpdateException) ex;
  -            msg.append("* BatchUpdateException during execution of sql-statement:");
  +            if(message != null)
  +            {
  +                msg.append("* ").append(message);
  +            }
  +            else
  +            {
  +                msg.append("* BatchUpdateException during execution of sql-statement:");
  +            }
               msg.append(eol).append("Batch update count is '").append(tmp.getUpdateCounts()).append("'");
           }
           else if(ex instanceof SQLWarning)
           {
  -            msg.append("* SQLWarning during execution of sql-statement:");
  +            if(message != null)
  +            {
  +                msg.append("* ").append(message);
  +            }
  +            else
  +            {
  +                msg.append("* SQLWarning during execution of sql-statement:");
  +            }
           }
           else
           {
  -            msg.append("* SQLException during execution of sql-statement:");
  +            if(message != null)
  +            {
  +                msg.append("* ").append(message);
  +            }
  +            else
  +            {
  +                msg.append("* SQLException during execution of sql-statement:");
  +            }
           }
   
           if(sql != null)
           {
               msg.append(eol).append("sql statement was '").append(sql).append("'");
           }
  -        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 [");
  -
  -        String stateCode = ex.getSQLState();
  -        if("23000".equalsIgnoreCase(stateCode)) msg.append(stateCode).append("=INTEGRITY
CONSTRAINT VIOLATION");
  -        else if("23001".equalsIgnoreCase(stateCode)) msg.append(stateCode).append("=RESTRICT
VIOLATION");
  -        else if("23502".equalsIgnoreCase(stateCode)) msg.append(stateCode).append("=NOT
NULL VIOLATION");
  -        else if("23503".equalsIgnoreCase(stateCode)) msg.append(stateCode).append("=FOREIGN
KEY VIOLATION");
  -        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("]");
  +        String stateCode = null;
  +        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 [");
  +
  +            stateCode = ex.getSQLState();
  +            if("23000".equalsIgnoreCase(stateCode)) msg.append(stateCode).append("=INTEGRITY
CONSTRAINT VIOLATION");
  +            else if("23001".equalsIgnoreCase(stateCode)) msg.append(stateCode).append("=RESTRICT
VIOLATION");
  +            else if("23502".equalsIgnoreCase(stateCode)) msg.append(stateCode).append("=NOT
NULL VIOLATION");
  +            else if("23503".equalsIgnoreCase(stateCode)) msg.append(stateCode).append("=FOREIGN
KEY VIOLATION");
  +            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)
           {
  @@ -170,12 +232,15 @@
           // message string for PB exception
           String shortMsg = msg.toString();
   
  -        // 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);
  +        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
  
  
  

---------------------------------------------------------------------
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