Skip to content

Commit f16fd44

Browse files
author
robdu
committed
remedy poor exception handling with java reflection
1 parent f311eb6 commit f16fd44

File tree

6 files changed

+46
-7
lines changed

6 files changed

+46
-7
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'java'
22
apply plugin: 'idea'
33
apply plugin: 'maven'
44

5-
project.version = "1.3.4"
5+
project.version = "1.3.5"
66
project.group = 'com.github.codingchili.chili-core'
77

88
subprojects {

core/main/java/com/codingchili/core/protocol/Protocol.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.codingchili.core.protocol;
22

3+
import com.codingchili.core.context.CoreRuntimeException;
34
import io.vertx.core.Future;
45
import io.vertx.core.buffer.Buffer;
56

@@ -226,12 +227,25 @@ private void wrap(String route, Receiver<RequestType> handler, Method method, Ro
226227
@SuppressWarnings("unchecked")
227228
private <E> E invokeMethod(Method method, Object instance, Object argument) {
228229
try {
229-
return (E) method.invoke(instance, argument);
230+
if (method.getParameterCount() == 0) {
231+
return (E) method.invoke(instance);
232+
} else {
233+
return (E) method.invoke(instance, argument);
234+
}
230235
} catch (IllegalAccessException | InvocationTargetException e) {
231-
throw new RuntimeException(e);
236+
throw throwAny(e.getCause()); // thrown inside throwAny method.
232237
}
233238
}
234239

240+
public static RuntimeException throwAny(final Throwable throwable) {
241+
Protocol.<RuntimeException>throwAsUnchecked(throwable);
242+
throw new AssertionError("Internal error in exception rewrite.");
243+
}
244+
245+
public static <T extends Exception> void throwAsUnchecked(Throwable throwable) throws T {
246+
throw (T) throwable;
247+
}
248+
235249
/**
236250
* Registers a handler for the given route.
237251
* <p>

core/test/java/com/codingchili/core/protocol/AnnotatedRouter.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.codingchili.core.protocol;
22

3+
import com.codingchili.core.context.CoreException;
4+
import com.codingchili.core.context.CoreRuntimeException;
35
import io.vertx.core.Future;
46

57
import com.codingchili.core.listener.CoreHandler;
@@ -51,6 +53,11 @@ public void customRoleOnRoute(Request request) {
5153
request.write(customRoleOnRoute);
5254
}
5355

56+
@Api(PUBLIC)
57+
public void throwMappedException(Request request) {
58+
throw new CoreRuntimeException("Core runtime exception retains message");
59+
}
60+
5461
@Override
5562
public void handle(Request request) {
5663
// unused: protocol is called directly by tests.

core/test/java/com/codingchili/core/protocol/ProtocolTest.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.codingchili.core.protocol;
22

3+
import com.codingchili.core.context.CoreRuntimeException;
34
import com.codingchili.core.listener.CoreHandler;
45
import com.codingchili.core.listener.Request;
56
import com.codingchili.core.protocol.exception.AuthorizationRequiredException;
@@ -36,7 +37,8 @@ public abstract class ProtocolTest {
3637
static final String specialRoute = "specialRoute";
3738
static final String CUSTOM_ROLE = "CUSTOM_ROLE";
3839
private static final String MISSING = "missing";
39-
private static final int ROUTE_COUNT = 7;
40+
public static final String mappedException = "throwMappedException";
41+
private static final int ROUTE_COUNT = 8;
4042

4143
static {
4244
RoleMap.put(CUSTOM_ROLE, new RoleType() {
@@ -70,7 +72,6 @@ public void setUp() {
7072
@Test
7173
public void testHandlerMissing(TestContext test) throws Exception {
7274
Async async = test.async();
73-
7475
try {
7576
protocol.get(MISSING);
7677
test.fail("Should throw handler missing exception.");
@@ -133,6 +134,18 @@ public void testCustomRouteName(TestContext test) {
133134
protocol.get(specialRoute, PUBLIC).submit(onWrite(test.async(), specialRoute));
134135
}
135136

137+
@Test
138+
public void testMappedException(TestContext test) {
139+
Async async = test.async();
140+
try {
141+
protocol.get(mappedException, PUBLIC)
142+
.submit(onWrite(async, mappedException));
143+
test.fail("Did not throw exception.");
144+
} catch (CoreRuntimeException e) {
145+
async.complete();
146+
}
147+
}
148+
136149
@Test
137150
public void testMultipleUsersHasAccess(TestContext test) {
138151
protocol.get(multipleUserRoute, USER).submit(onWrite(test.async(), multipleUserRoute));

core/test/java/com/codingchili/core/protocol/SimpleBuilderRouter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.codingchili.core.protocol;
22

3+
import com.codingchili.core.context.CoreRuntimeException;
34
import io.vertx.core.Future;
45

56
import com.codingchili.core.listener.CoreHandler;
@@ -26,6 +27,7 @@ public SimpleBuilderRouter() {
2627
.use(adminRoleRoute, this::adminRoleRoute, ADMIN)
2728
.use(userRoleRoute, this::userRoleRoute, USER)
2829
.use(specialRoute, this::customRouteName)
30+
.use(mappedException, this::throwMappedExcepetion)
2931
.use(multipleUserRoute, this::multipleUserRoute, USER, ADMIN)
3032
.use(customRoleOnRoute, this::customRoleOnRoute, RoleMap.get(CUSTOM_ROLE));
3133
}
@@ -42,6 +44,10 @@ public Protocol<Request> getProtocol() {
4244
return protocol;
4345
}
4446

47+
public void throwMappedExcepetion(Request request) {
48+
throw new CoreRuntimeException("mapped runtime exception");
49+
}
50+
4551
public void documentedRoute(Request request) {
4652
request.write(documentedRoute);
4753
}

core/test/java/com/codingchili/core/storage/QueryTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void setUp() {
4646

4747
@Test
4848
public void testGenerateQueryString(TestContext test) {
49-
String query = new Query().on("cat.type")
49+
String query = new Query<>().on("cat.type")
5050
.in("siamese", "perser", "ragdoll")
5151
.and("cat.color").equalTo("white")
5252
.or("cat.lifestyle").in("amphibians", "wateranimal").matches("[water].*")
@@ -57,7 +57,6 @@ public void testGenerateQueryString(TestContext test) {
5757
.toString();
5858
}
5959

60-
6160
@Test
6261
public void testParseQueryString(TestContext test) {
6362
QueryBuilder<Account> builder = new Query<>();

0 commit comments

Comments
 (0)