diff --git a/src/main/java/com/ljunggren/storm/StormRepository.java b/src/main/java/com/ljunggren/storm/StormRepository.java index 88dc88a..4b4d760 100644 --- a/src/main/java/com/ljunggren/storm/StormRepository.java +++ b/src/main/java/com/ljunggren/storm/StormRepository.java @@ -17,6 +17,7 @@ import com.ljunggren.storm.crud.QueryChain; import com.ljunggren.storm.crud.SelectQuery; import com.ljunggren.storm.crud.UpdateQuery; +import com.ljunggren.storm.exceptions.StormException; public class StormRepository implements InvocationHandler { @@ -54,7 +55,11 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Iterator annotations = Arrays.stream(method.getAnnotations()).iterator(); Type returnType = method.getGenericReturnType(); - return execute(annotations, args, returnType); + try { + return execute(annotations, args, returnType); + } catch (SQLException e) { + throw new StormException(e.getMessage()); + } } private Object execute(Iterator annotations, Object[] args, Type returnType) throws SQLException { diff --git a/src/main/java/com/ljunggren/storm/exceptions/StormException.java b/src/main/java/com/ljunggren/storm/exceptions/StormException.java new file mode 100644 index 0000000..b126ad6 --- /dev/null +++ b/src/main/java/com/ljunggren/storm/exceptions/StormException.java @@ -0,0 +1,11 @@ +package com.ljunggren.storm.exceptions; + +public class StormException extends RuntimeException { + + private static final long serialVersionUID = 1478572986254603169L; + + public StormException(String message) { + super(message); + } + +} diff --git a/src/test/java/com/ljunggren/storm/crud/DeleteQueryTest.java b/src/test/java/com/ljunggren/storm/crud/DeleteQueryTest.java index 894b6d8..ba3208a 100644 --- a/src/test/java/com/ljunggren/storm/crud/DeleteQueryTest.java +++ b/src/test/java/com/ljunggren/storm/crud/DeleteQueryTest.java @@ -16,6 +16,7 @@ import com.ljunggren.storm.annotation.Select; import com.ljunggren.storm.context.Context; import com.ljunggren.storm.context.ContextFactory; +import com.ljunggren.storm.exceptions.StormException; public class DeleteQueryTest { @@ -31,6 +32,9 @@ @Delete public int deleteAll(TestUser... users); + @Delete(sql = "nonsense") + public int nonsense(); + @Select(sql = "select count(*) from users") public long count(); @@ -79,5 +83,11 @@ assertEquals(2, deletes); assertTrue(beforeCount > afterCount); } + + @Test(expected = StormException.class) + public void nonsenseTest() { + UserRepository repository = StormRepository.newInstance(UserRepository.class); + repository.nonsense(); + } } diff --git a/src/test/java/com/ljunggren/storm/crud/InsertQueryTest.java b/src/test/java/com/ljunggren/storm/crud/InsertQueryTest.java index a67093c..e5d67e3 100644 --- a/src/test/java/com/ljunggren/storm/crud/InsertQueryTest.java +++ b/src/test/java/com/ljunggren/storm/crud/InsertQueryTest.java @@ -16,6 +16,7 @@ import com.ljunggren.storm.annotation.Select; import com.ljunggren.storm.context.Context; import com.ljunggren.storm.context.ContextFactory; +import com.ljunggren.storm.exceptions.StormException; public class InsertQueryTest { @@ -31,6 +32,9 @@ @Insert public int insertAll(TestUser... users); + @Insert(sql = "nonsense") + public int nonsense(); + @Select(sql = "select count(*) from users") public long count(); @@ -79,5 +83,11 @@ assertEquals(2, inserts); assertTrue(beforeCount < afterCount); } + + @Test(expected = StormException.class) + public void nonsenseTest() { + UserRepository repository = StormRepository.newInstance(UserRepository.class); + repository.nonsense(); + } } diff --git a/src/test/java/com/ljunggren/storm/crud/SelectQueryTest.java b/src/test/java/com/ljunggren/storm/crud/SelectQueryTest.java index 4124745..9331df0 100644 --- a/src/test/java/com/ljunggren/storm/crud/SelectQueryTest.java +++ b/src/test/java/com/ljunggren/storm/crud/SelectQueryTest.java @@ -18,6 +18,7 @@ import com.ljunggren.storm.annotation.Select; import com.ljunggren.storm.context.Context; import com.ljunggren.storm.context.ContextFactory; +import com.ljunggren.storm.exceptions.StormException; public class SelectQueryTest { @@ -41,6 +42,10 @@ @Select(sql = "select * from users") public Set fetchAll(); + + @Select(sql = "nonsense") + public int nonsense(); + } @Before @@ -112,4 +117,9 @@ assertTrue(users.size() > 0); } + @Test(expected = StormException.class) + public void nonsenseTest() { + UserRepository repository = StormRepository.newInstance(UserRepository.class); + repository.nonsense(); + } } diff --git a/src/test/java/com/ljunggren/storm/crud/UpdateQueryTest.java b/src/test/java/com/ljunggren/storm/crud/UpdateQueryTest.java index 0065449..e2296a3 100644 --- a/src/test/java/com/ljunggren/storm/crud/UpdateQueryTest.java +++ b/src/test/java/com/ljunggren/storm/crud/UpdateQueryTest.java @@ -15,6 +15,7 @@ import com.ljunggren.storm.annotation.Update; import com.ljunggren.storm.context.Context; import com.ljunggren.storm.context.ContextFactory; +import com.ljunggren.storm.exceptions.StormException; public class UpdateQueryTest { @@ -30,6 +31,9 @@ @Update public int updateAll(TestUser... users); + @Update(sql = "nonsense") + public int nonsense(); + @Select(sql = "select * from users where id = ?") public TestUser findById(int id); @@ -78,5 +82,11 @@ assertEquals(888, user1.getEmployeeID()); assertEquals(999, user2.getEmployeeID()); } + + @Test(expected = StormException.class) + public void nonsenseTest() { + UserRepository repository = StormRepository.newInstance(UserRepository.class); + repository.nonsense(); + } }