empire-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From doeb...@apache.org
Subject svn commit: r1147166 - in /incubator/empire-db/branches/EMPIREDB-99/empire-db/src: main/java/org/apache/empire/ main/java/org/apache/empire/commons/ main/java/org/apache/empire/db/ test/java/org/apache/empire/
Date Fri, 15 Jul 2011 14:42:10 GMT
Author: doebele
Date: Fri Jul 15 14:42:09 2011
New Revision: 1147166

URL: http://svn.apache.org/viewvc?rev=1147166&view=rev
Log:
EMPIREDB-99 first step

Modified:
    incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/EmpireException.java
    incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/commons/ErrorObject.java
    incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBCommand.java
    incubator/empire-db/branches/EMPIREDB-99/empire-db/src/test/java/org/apache/empire/EmpireExceptionTest.java

Modified: incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/EmpireException.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/EmpireException.java?rev=1147166&r1=1147165&r2=1147166&view=diff
==============================================================================
--- incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/EmpireException.java
(original)
+++ incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/EmpireException.java
Fri Jul 15 14:42:09 2011
@@ -18,11 +18,13 @@
  */
 package org.apache.empire;
 
-import org.apache.empire.commons.ErrorInfo;
+import java.text.MessageFormat;
+
 import org.apache.empire.commons.ErrorObject;
 import org.apache.empire.commons.ErrorType;
-
-import java.io.Serializable;
+import org.apache.empire.commons.Errors;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * This exception type is used for all empire errors.<br>
@@ -31,16 +33,123 @@ import java.io.Serializable;
  */
 public final class EmpireException extends RuntimeException
 {
+    // Logger
+    private static final Logger log = LoggerFactory.getLogger(ErrorObject.class);
+    
     private static final long serialVersionUID = 1L;
     
     private final ErrorType errorType;
-    private final ErrorInfo errorObject;
-    private final String errorObjectClassname;
+    private final Object[]  errorParams;
+    // private final String errorSourceClassname;
+    
+    private static String formatErrorMessage(final ErrorType errType, final Object[] params)
+    {
+        // check error code
+        if (errType == Errors.None)
+        {   // Must supply a valid error code
+            log.error("error function called with invalid error Code.");
+            return formatErrorMessage(Errors.Internal, new Object[] { "Invalid Error Code"
});
+        }
+        // Check parameter count
+        int paramCount = (params!=null) ? params.length : 0;
+        if (paramCount!= errType.getNumParams())
+        {   // Number of parameters does not match
+            log.warn("Invalid Number of arguments supplied for error " + errType.getKey()

+                   + "\nArguments supplied= " + String.valueOf(paramCount) + "; Arguments
expected= " + String.valueOf(errType.getNumParams()));
+        }
+        // Log Error
+        String msg = MessageFormat.format(errType.getMessagePattern(), params);
+        return msg;
+    }
+    
+    private static Object[] paramsFromThrowable(final Throwable exptn)
+    {
+        // Exception
+        String type  = exptn.getClass().getName();
+        if (type.startsWith("java.lang."))
+            type = type.substring("java.lang.".length());
+        // The message
+        String msg   = exptn.getMessage();
+        // Prepare stack trace
+        StackTraceElement[] stack = exptn.getStackTrace();
+        String pos = (stack!=null) ? stack[0].toString() : "{unknown}";
+        // Create Error
+        return new Object[] { type, msg, pos };
+    }
+    
+    private static Object[] normalizeParams(final Object[] params)
+    {
+        if (params!=null && params.length>0)
+        {   // convert complex params form object[] to string[]
+            for (int i=0; i<params.length; i++)
+            {   // convert to String
+                Object o = params[i]; 
+                if (o==null || (o instanceof String))
+                    continue;
+                if (o.getClass().isPrimitive())
+                    continue;
+                // Convert to String
+                if (o instanceof RuntimeException)
+                    params[i] = ((RuntimeException)o).getMessage();
+                else 
+                    params[i] = String.valueOf(o.toString());
+            }
+        }
+        return params;
+    }
+
+    /**
+     * Constructor
+     * @param errType
+     * @param params
+     */
+    public EmpireException(final ErrorType errType, final Object... params)
+    {
+        super(formatErrorMessage(errType, params));
+        // save type and params for custom message formatting
+        this.errorType = errType;
+        this.errorParams = normalizeParams(params);
+        // done
+        log();
+    }
+
+    /**
+     * Constructor
+     * @param other
+     */
+    public EmpireException(final EmpireException other)
+    {
+        super(other.getMessage(), other);
+        // save type and params for custom message formatting
+        this.errorType   = other.getErrorType();
+        this.errorParams = other.getErrorParams(); 
+        // done
+        log();
+    }
+    
+    /**
+     * Constructor
+     * @param cause
+     */
+    public EmpireException(final Throwable cause)
+    {
+        super(formatErrorMessage(Errors.Exception, paramsFromThrowable(cause)), cause);
+        // save type and params for custom message formatting
+        this.errorType   = Errors.Exception;
+        this.errorParams = paramsFromThrowable(cause); 
+        // done
+        log();
+    }
+    
+    private void log()
+    {
+        if (log.isInfoEnabled())
+            log.info("Error '" + this.getMessage() + "' has been set.");
+    }
     
     /**
      * creates an empire exception from an error object.
      * @param errorObject
-     */
     public EmpireException(final ErrorInfo errorObject)
     {
         super(errorObject.getErrorMessage());
@@ -55,6 +164,7 @@ public final class EmpireException exten
     {   // Return Object class name and error message
         return errorObjectClassname + ": " + getMessage();
     }
+     */
 
     /**
      * The type of error that occurred
@@ -66,17 +176,15 @@ public final class EmpireException exten
         return errorType;
     }
 
-    /**
-     * @return the object that caused the error
-     */
-    public ErrorInfo getErrorObject()
+    
+    public Object[] getErrorParams()
     {
-        return errorObject;
+        return errorParams;
     }
-
+    
+    
     /**
      * A serializable version of {@link ErrorInfo}.
-     */
     private static class DeepCopyErrorInfo implements ErrorInfo, Serializable {
         private static final long serialVersionUID = 1L;
         
@@ -86,10 +194,7 @@ public final class EmpireException exten
         private final String errorSource;
         private final String errorMessage;
 
-        /**
-         * Copy ctor.
-         * @param errorInfo
-         */
+        // Copy ctor.
         DeepCopyErrorInfo(ErrorInfo errorInfo) {
             this.hasError = errorInfo.hasError();
             this.errorType = errorInfo.getErrorType();
@@ -134,4 +239,5 @@ public final class EmpireException exten
             return errorMessage;
         }
     }
+     */
 }

Modified: incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/commons/ErrorObject.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/commons/ErrorObject.java?rev=1147166&r1=1147165&r2=1147166&view=diff
==============================================================================
--- incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/commons/ErrorObject.java
(original)
+++ incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/commons/ErrorObject.java
Fri Jul 15 14:42:09 2011
@@ -247,7 +247,7 @@ public abstract class ErrorObject implem
         info.errSource =(source!=null) ? source.getClass().getName() : getClass().getName();
         // Use Exceptions
         if (exceptionsEnabled)
-            throw new EmpireException(this);
+            throw new EmpireException(errType, params);
         // return Error Status
         return false;
     }

Modified: incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBCommand.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBCommand.java?rev=1147166&r1=1147165&r2=1147166&view=diff
==============================================================================
--- incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBCommand.java
(original)
+++ incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBCommand.java
Fri Jul 15 14:42:09 2011
@@ -171,9 +171,7 @@ public abstract class DBCommand extends 
         int index = cmdParams.indexOf(param);
         if (index < paramUsageCount)
         {   // Error: parameter probably used twice in statement!
-            String msg = "A parameter may only be used once in a command.";
-            error(Errors.Internal, msg);
-            throw new EmpireException(this);
+            throw new EmpireException(Errors.Internal, "A parameter may only be used once
in a command.");
         }
         if (index > paramUsageCount)
         {   // Correct parameter order

Modified: incubator/empire-db/branches/EMPIREDB-99/empire-db/src/test/java/org/apache/empire/EmpireExceptionTest.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/branches/EMPIREDB-99/empire-db/src/test/java/org/apache/empire/EmpireExceptionTest.java?rev=1147166&r1=1147165&r2=1147166&view=diff
==============================================================================
--- incubator/empire-db/branches/EMPIREDB-99/empire-db/src/test/java/org/apache/empire/EmpireExceptionTest.java
(original)
+++ incubator/empire-db/branches/EMPIREDB-99/empire-db/src/test/java/org/apache/empire/EmpireExceptionTest.java
Fri Jul 15 14:42:09 2011
@@ -18,71 +18,27 @@
  */
 package org.apache.empire;
 
-import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
 
-import org.apache.empire.commons.ErrorInfo;
-import org.apache.empire.commons.ErrorObject;
-import org.apache.empire.commons.ErrorType;
 import org.apache.empire.commons.Errors;
 import org.junit.Before;
 import org.junit.Test;
 
 public class EmpireExceptionTest
 {
-
-    private final class MockErrorInfo implements ErrorInfo
-    {
-
-        public Object[] getErrorParams()
-        {
-            return new Object[] { "JUnit", "Test" };
-        }
-
-        public String getErrorSource()
-        {
-            return "JUnitSource";
-        }
-
-        public ErrorType getErrorType()
-        {
-            return Errors.IllegalFormat;
-        }
-
-        public boolean hasError()
-        {
-            return true;
-        }
-
-        public String getErrorMessage()
-        {
-            return "JUnit error.";
-        }
-    }
-
-    private final class MockErrorObject extends ErrorObject
-    {
-        public MockErrorObject()
-        {
-            super(new MockErrorInfo());
-        }
-    }
-
     private EmpireException exception;
-    private ErrorObject     errorObject;
 
     @Before
     public void setupException()
     {
-        ErrorObject.setExceptionsEnabled(false);
-        this.errorObject = new MockErrorObject();
-        this.exception = new EmpireException(errorObject);
+        this.exception = new EmpireException(Errors.InvalidArg, -1, "param");
     }
 
     @Test
     public void testToString()
     {
-        String expected = errorObject.getClass().getName() + ": The format of JUnit is invalid
for Test";
+        String expected = "Invalid Argument -1 for parameter param.";
         assertEquals(expected, exception.toString());
     }
 
@@ -95,11 +51,9 @@ public class EmpireExceptionTest
     @Test
     public void testGetErrorObject()
     {
-        assertEquals(errorObject.getErrorMessage(), exception.getErrorObject().getErrorMessage());
-        assertEquals(errorObject.getErrorSource(), exception.getErrorObject().getErrorSource());
-        assertEquals(errorObject.getErrorType(), exception.getErrorObject().getErrorType());
-        assertEquals(errorObject.hasError(), exception.getErrorObject().hasError());
-        assertArrayEquals(errorObject.getErrorParams(), exception.getErrorObject().getErrorParams());
+        assertEquals(exception.getMessage(),   "Invalid Argument -1 for parameter param.");
+        assertEquals(exception.getErrorType(), Errors.InvalidArg);
+        assertArrayEquals(exception.getErrorParams(), new Object[]{ -1, "param" });
     }
 
 }



Mime
View raw message