openjpa-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Pinaki Poddar <ppod...@apache.org>
Subject Re: Exception translation by EntityManager facade
Date Wed, 28 May 2008 22:52:26 GMT

Hi,
  Please find the following suggested change on exception translation in
PersistenceExceptions class. The idea is to apply further heuristics on
available exception to translate, failing which results in existing
behavior.

   Because this is heuristic by nature, your comment will be much
appreciated (and required for me to commit the changes):



Index:
openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceExceptions.java
===================================================================
---
openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceExceptions.java
(revision 657964)
+++
openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceExceptions.java
(working copy)
@@ -22,6 +22,8 @@

 import org.apache.openjpa.kernel.Broker;
 import org.apache.openjpa.util.Exceptions;
+import org.apache.openjpa.util.ObjectExistsException;
+import org.apache.openjpa.util.ObjectNotFoundException;
 import org.apache.openjpa.util.OpenJPAException;
 import org.apache.openjpa.util.RuntimeExceptionTranslator;
 import org.apache.openjpa.util.StoreException;
@@ -167,6 +169,12 @@

     /**
      * Translate the given user exception.
+     * If a {link {@link OpenJPAException#getSubtype() sub type} is set on
the
+     * given exception then a corresponding facade-level exception i.e. the
+     * exceptions that inherit JPA-defined exceptions is generated.
+     * If given exception is not further classified to a sub type, then
+     * an [@link {@link #translateInternalException(OpenJPAException)}
attempt}
+     * is made to translate the given OpenJPAException by its internal
cause.
      */
     private static Exception translateUserException(OpenJPAException ke) {
         Exception e;
@@ -193,13 +201,39 @@
                         getFailedObject(ke), ke.isFatal());
                 break;
             default:
-                e = new org.apache.openjpa.persistence.ArgumentException
-                    (ke.getMessage(), getNestedThrowables(ke),
-                        getFailedObject(ke), ke.isFatal());
+               e = translateCause(ke);
         }
         e.setStackTrace(ke.getStackTrace());
         return e;
     }
+
+    /**
+     * Translate to a facade-level exception if the given exception
+     *     a) has a cause i.e. one and only nested Throwable
+     * and b) that cause is one of the known internal exception which has a
+     *        direct facade-level counterpart
+     *        (for example, ObjectNotFoundException can be translated to
+     *         EntityNotFoundException).
+     * If the above conditions are not met then return generic
ArgumentException.
+     *
+     * In either case, preserve all the details.
+     */
+    private static Exception translateCause(OpenJPAException ke) {
+       Throwable cause = ke.getCause();
+       if (cause instanceof ObjectNotFoundException) {
+               return new EntityNotFoundException(
+                       ke.getMessage(), getNestedThrowables(ke),
+               getFailedObject(ke), ke.isFatal());
+       } else if (cause instanceof ObjectExistsException) {
+               return new EntityExistsException(
+                       ke.getMessage(), getNestedThrowables(ke),
+               getFailedObject(ke), ke.isFatal());
+       } else {
+               return new org.apache.openjpa.persistence.ArgumentException(
+                       ke.getMessage(), getNestedThrowables(ke),
+                       getFailedObject(ke), ke.isFatal());
+       }
+    }

-- 
View this message in context: http://www.nabble.com/Exception-translation-by-EntityManager-facade-tp17521307p17523862.html
Sent from the OpenJPA Developers mailing list archive at Nabble.com.


Mime
View raw message