deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t...@apache.org
Subject [1/3] git commit: DELTASPIKE-448 Refactoring of exception handling
Date Thu, 28 Nov 2013 14:48:09 GMT
Updated Branches:
  refs/heads/master feba6a764 -> c3f89b2fc


DELTASPIKE-448 Refactoring of exception handling


Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/4e8d5831
Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/4e8d5831
Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/4e8d5831

Branch: refs/heads/master
Commit: 4e8d58316ee707cda093fe24eb1c6b6286b90d04
Parents: f3c00d8
Author: Thomas Hug <Thomas.Hug@ctp-consulting.com>
Authored: Fri Nov 22 17:52:30 2013 +0100
Committer: Thomas Hug <Thomas.Hug@ctp-consulting.com>
Committed: Thu Nov 28 14:38:25 2013 +0100

----------------------------------------------------------------------
 .../data/api/QueryInvocationException.java      | 71 +++++++++++++++++++
 .../data/spi/QueryInvocationContext.java        | 12 ++++
 .../data/impl/builder/DelegateQueryBuilder.java | 13 +++-
 .../impl/handler/CdiQueryInvocationContext.java |  7 ++
 .../impl/handler/QueryInvocationException.java  | 72 --------------------
 .../data/impl/handler/EntityManagerTest.java    |  1 +
 .../data/impl/handler/QueryHandlerTest.java     |  5 +-
 .../data/test/util/TestDeployments.java         |  4 +-
 8 files changed, 108 insertions(+), 77 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/4e8d5831/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/QueryInvocationException.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/QueryInvocationException.java
b/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/QueryInvocationException.java
new file mode 100644
index 0000000..1e870c2
--- /dev/null
+++ b/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/QueryInvocationException.java
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.deltaspike.data.api;
+
+import java.lang.reflect.Method;
+
+import org.apache.deltaspike.data.spi.QueryInvocationContext;
+
+public class QueryInvocationException extends RuntimeException
+{
+
+    private static final long serialVersionUID = 1L;
+
+    public QueryInvocationException(Throwable t, QueryInvocationContext context)
+    {
+        super(createMessage(context, t), t);
+    }
+
+    public QueryInvocationException(String message, QueryInvocationContext context)
+    {
+        super(createMessage(context));
+    }
+
+    public QueryInvocationException(Throwable t, Class<?> proxy, Method method)
+    {
+        super(createMessage(proxy, method, t), t);
+    }
+
+    private static String createMessage(QueryInvocationContext context)
+    {
+        StringBuilder builder = new StringBuilder();
+        builder.append("Failed calling Repository: [");
+        builder.append("Repository=").append(context.getRepositoryClass().getName()).append(",");
+        builder.append("entity=").append(context.getEntityClass().getName()).append(",");
+        builder.append("method=").append(context.getMethod().getName()).append(",");
+        return builder.toString();
+    }
+
+    private static String createMessage(QueryInvocationContext context, Throwable t)
+    {
+        StringBuilder builder = new StringBuilder(createMessage(context));
+        builder.append("exception=").append(t.getClass()).append(",message=").append(t.getMessage());
+        return builder.toString();
+    }
+
+    private static String createMessage(Class<?> repoClass, Method method, Throwable
t)
+    {
+        StringBuilder builder = new StringBuilder();
+        builder.append("Exception calling Repository: [");
+        builder.append("Repository=").append(repoClass).append(",");
+        builder.append("method=").append(method.getName()).append("],");
+        builder.append("exception=").append(t.getClass()).append(",message=").append(t.getMessage());
+        return builder.toString();
+    }
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/4e8d5831/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/spi/QueryInvocationContext.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/spi/QueryInvocationContext.java
b/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/spi/QueryInvocationContext.java
index 6ee4836..c533ba9 100644
--- a/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/spi/QueryInvocationContext.java
+++ b/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/spi/QueryInvocationContext.java
@@ -18,6 +18,8 @@
  */
 package org.apache.deltaspike.data.spi;
 
+import java.lang.reflect.Method;
+
 import javax.persistence.EntityManager;
 
 /**
@@ -44,4 +46,14 @@ public interface QueryInvocationContext
      */
     boolean isNew(Object entity);
 
+    /**
+     * The type of the repository currently accessed.
+     */
+    Class<?> getRepositoryClass();
+
+    /**
+     * The repository method currently executed.
+     */
+    Method getMethod();
+
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/4e8d5831/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/DelegateQueryBuilder.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/DelegateQueryBuilder.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/DelegateQueryBuilder.java
index 8ed2361..0668ce7 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/DelegateQueryBuilder.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/DelegateQueryBuilder.java
@@ -27,10 +27,11 @@ import javax.enterprise.context.spi.CreationalContext;
 import javax.enterprise.inject.spi.Bean;
 import javax.enterprise.inject.spi.BeanManager;
 import javax.inject.Inject;
+import javax.persistence.PersistenceException;
 
 import org.apache.deltaspike.core.api.provider.BeanProvider;
+import org.apache.deltaspike.data.api.QueryInvocationException;
 import org.apache.deltaspike.data.impl.handler.CdiQueryInvocationContext;
-import org.apache.deltaspike.data.impl.handler.QueryInvocationException;
 import org.apache.deltaspike.data.impl.meta.MethodType;
 import org.apache.deltaspike.data.impl.meta.QueryInvocation;
 import org.apache.deltaspike.data.impl.util.bean.BeanDestroyable;
@@ -53,6 +54,10 @@ public class DelegateQueryBuilder extends QueryBuilder
                 return invoke(delegate, context);
             }
         }
+        catch (PersistenceException e)
+        {
+            throw e;
+        }
         catch (Exception e)
         {
             throw new QueryInvocationException(e, context);
@@ -108,7 +113,11 @@ public class DelegateQueryBuilder extends QueryBuilder
         }
         catch (InvocationTargetException e)
         {
-            throw new RuntimeException(e);
+            if (e.getCause() != null && e.getCause() instanceof PersistenceException)
+            {
+                throw (PersistenceException) e.getCause();
+            }
+            throw new QueryInvocationException(e, context);
         }
         catch (IllegalAccessException e)
         {

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/4e8d5831/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/CdiQueryInvocationContext.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/CdiQueryInvocationContext.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/CdiQueryInvocationContext.java
index 4549cb6..8900d68 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/CdiQueryInvocationContext.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/CdiQueryInvocationContext.java
@@ -105,11 +105,18 @@ public class CdiQueryInvocationContext implements QueryInvocationContext
         return entityClass;
     }
 
+    @Override
+    public Class<?> getRepositoryClass()
+    {
+        return repoMethod.getRepository().getRepositoryClass();
+    }
+
     public Object proceed() throws Exception
     {
         return method.invoke(proxy, args);
     }
 
+    @Override
     public Method getMethod()
     {
         return method;

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/4e8d5831/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/QueryInvocationException.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/QueryInvocationException.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/QueryInvocationException.java
deleted file mode 100644
index 2b8c216..0000000
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/QueryInvocationException.java
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.deltaspike.data.impl.handler;
-
-import java.lang.reflect.Method;
-
-public class QueryInvocationException extends RuntimeException
-{
-
-    private static final long serialVersionUID = 1L;
-
-    public QueryInvocationException(Throwable t, CdiQueryInvocationContext context)
-    {
-        super(createMessage(context, t), t);
-    }
-
-    public QueryInvocationException(String message, CdiQueryInvocationContext context)
-    {
-        super(createMessage(context));
-    }
-
-    public QueryInvocationException(Throwable t, Class<?> proxy, Method method)
-    {
-        super(createMessage(proxy, method, t), t);
-    }
-
-    private static String createMessage(CdiQueryInvocationContext context)
-    {
-        StringBuilder builder = new StringBuilder();
-        builder.append("Failed calling Repository: [");
-        builder.append("Repository=").append(context.getRepositoryMethod()
-                .getRepository().getRepositoryClass().getName()).append(",");
-        builder.append("entity=").append(context.getEntityClass().getName()).append(",");
-        builder.append("method=").append(context.getMethod().getName()).append(",");
-        builder.append("query=").append(context.getQueryString()).append("],");
-        return builder.toString();
-    }
-
-    private static String createMessage(CdiQueryInvocationContext context, Throwable t)
-    {
-        StringBuilder builder = new StringBuilder(createMessage(context));
-        builder.append("exception=").append(t.getClass()).append(",message=").append(t.getMessage());
-        return builder.toString();
-    }
-
-    private static String createMessage(Class<?> proxy, Method method, Throwable t)
-    {
-        StringBuilder builder = new StringBuilder();
-        builder.append("Exception calling Repository: [");
-        builder.append("Repository=").append(proxy).append(",");
-        builder.append("method=").append(method.getName()).append("],");
-        builder.append("exception=").append(t.getClass()).append(",message=").append(t.getMessage());
-        return builder.toString();
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/4e8d5831/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/EntityManagerTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/EntityManagerTest.java
b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/EntityManagerTest.java
index 18f2399..b49ae13 100644
--- a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/EntityManagerTest.java
+++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/EntityManagerTest.java
@@ -26,6 +26,7 @@ import java.util.List;
 
 import javax.inject.Inject;
 
+import org.apache.deltaspike.data.api.QueryInvocationException;
 import org.apache.deltaspike.data.test.domain.Simple;
 import org.apache.deltaspike.data.test.service.SimpleRepositoryWithEntityManager;
 import org.apache.deltaspike.data.test.service.SimpleRepositoryWithEntityManagerResolver;

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/4e8d5831/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/QueryHandlerTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/QueryHandlerTest.java
b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/QueryHandlerTest.java
index 712eeb5..4904fda 100644
--- a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/QueryHandlerTest.java
+++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/QueryHandlerTest.java
@@ -29,6 +29,7 @@ import java.util.List;
 import javax.enterprise.inject.Produces;
 import javax.inject.Inject;
 import javax.persistence.EntityManager;
+import javax.persistence.NonUniqueResultException;
 import javax.persistence.PersistenceContext;
 
 import org.apache.deltaspike.data.test.TransactionalTestCase;
@@ -309,7 +310,7 @@ public class QueryHandlerTest extends TransactionalTestCase
         assertNull(result2);
     }
 
-    @Test(expected = QueryInvocationException.class)
+    @Test(expected = NonUniqueResultException.class)
     public void should_fail_optinal_query_by_name_with_nonunique()
     {
         // given
@@ -321,7 +322,7 @@ public class QueryHandlerTest extends TransactionalTestCase
         repo.findOptionalByName(name);
     }
 
-    @Test(expected = QueryInvocationException.class)
+    @Test(expected = NonUniqueResultException.class)
     public void should_fail_optinal_query_by_annotation_with_nonunique()
     {
         // given

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/4e8d5831/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/util/TestDeployments.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/util/TestDeployments.java
b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/util/TestDeployments.java
index f5ada14..64042f4 100755
--- a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/util/TestDeployments.java
+++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/util/TestDeployments.java
@@ -26,6 +26,7 @@ import org.apache.deltaspike.data.api.FirstResult;
 import org.apache.deltaspike.data.api.MaxResults;
 import org.apache.deltaspike.data.api.Modifying;
 import org.apache.deltaspike.data.api.Query;
+import org.apache.deltaspike.data.api.QueryInvocationException;
 import org.apache.deltaspike.data.api.QueryParam;
 import org.apache.deltaspike.data.api.QueryResult;
 import org.apache.deltaspike.data.api.Repository;
@@ -122,7 +123,8 @@ public abstract class TestDeployments
                 .addClasses(AbstractEntityRepository.class, Repository.class, EntityRepository.class,
                         FirstResult.class, MaxResults.class, Modifying.class,
                         Query.class, QueryParam.class, QueryResult.class,
-                        EntityManagerConfig.class, EntityManagerResolver.class, SingleResultType.class)
+                        EntityManagerConfig.class, EntityManagerResolver.class, SingleResultType.class,
+                        QueryInvocationException.class)
                 .addClasses(Criteria.class, QuerySelection.class, CriteriaSupport.class)
                 .addClasses(CreatedOn.class, CurrentUser.class, ModifiedBy.class, ModifiedOn.class)
                 .addClasses(MappingConfig.class, QueryInOutMapper.class)


Mime
View raw message