Skip to content

Commit

Permalink
remedy poor exception handling with java reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
robdu committed Sep 15, 2020
1 parent f311eb6 commit f16fd44
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 7 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'maven'

project.version = "1.3.4"
project.version = "1.3.5"
project.group = 'com.github.codingchili.chili-core'

subprojects {
Expand Down
18 changes: 16 additions & 2 deletions core/main/java/com/codingchili/core/protocol/Protocol.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.codingchili.core.protocol;

import com.codingchili.core.context.CoreRuntimeException;
import io.vertx.core.Future;
import io.vertx.core.buffer.Buffer;

Expand Down Expand Up @@ -226,12 +227,25 @@ private void wrap(String route, Receiver<RequestType> handler, Method method, Ro
@SuppressWarnings("unchecked")
private <E> E invokeMethod(Method method, Object instance, Object argument) {
try {
return (E) method.invoke(instance, argument);
if (method.getParameterCount() == 0) {
return (E) method.invoke(instance);
} else {
return (E) method.invoke(instance, argument);
}
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
throw throwAny(e.getCause()); // thrown inside throwAny method.
}
}

public static RuntimeException throwAny(final Throwable throwable) {
Protocol.<RuntimeException>throwAsUnchecked(throwable);
throw new AssertionError("Internal error in exception rewrite.");
}

public static <T extends Exception> void throwAsUnchecked(Throwable throwable) throws T {
throw (T) throwable;
}

/**
* Registers a handler for the given route.
* <p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.codingchili.core.protocol;

import com.codingchili.core.context.CoreException;
import com.codingchili.core.context.CoreRuntimeException;
import io.vertx.core.Future;

import com.codingchili.core.listener.CoreHandler;
Expand Down Expand Up @@ -51,6 +53,11 @@ public void customRoleOnRoute(Request request) {
request.write(customRoleOnRoute);
}

@Api(PUBLIC)
public void throwMappedException(Request request) {
throw new CoreRuntimeException("Core runtime exception retains message");
}

@Override
public void handle(Request request) {
// unused: protocol is called directly by tests.
Expand Down
17 changes: 15 additions & 2 deletions core/test/java/com/codingchili/core/protocol/ProtocolTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.codingchili.core.protocol;

import com.codingchili.core.context.CoreRuntimeException;
import com.codingchili.core.listener.CoreHandler;
import com.codingchili.core.listener.Request;
import com.codingchili.core.protocol.exception.AuthorizationRequiredException;
Expand Down Expand Up @@ -36,7 +37,8 @@ public abstract class ProtocolTest {
static final String specialRoute = "specialRoute";
static final String CUSTOM_ROLE = "CUSTOM_ROLE";
private static final String MISSING = "missing";
private static final int ROUTE_COUNT = 7;
public static final String mappedException = "throwMappedException";
private static final int ROUTE_COUNT = 8;

static {
RoleMap.put(CUSTOM_ROLE, new RoleType() {
Expand Down Expand Up @@ -70,7 +72,6 @@ public void setUp() {
@Test
public void testHandlerMissing(TestContext test) throws Exception {
Async async = test.async();

try {
protocol.get(MISSING);
test.fail("Should throw handler missing exception.");
Expand Down Expand Up @@ -133,6 +134,18 @@ public void testCustomRouteName(TestContext test) {
protocol.get(specialRoute, PUBLIC).submit(onWrite(test.async(), specialRoute));
}

@Test
public void testMappedException(TestContext test) {
Async async = test.async();
try {
protocol.get(mappedException, PUBLIC)
.submit(onWrite(async, mappedException));
test.fail("Did not throw exception.");
} catch (CoreRuntimeException e) {
async.complete();
}
}

@Test
public void testMultipleUsersHasAccess(TestContext test) {
protocol.get(multipleUserRoute, USER).submit(onWrite(test.async(), multipleUserRoute));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.codingchili.core.protocol;

import com.codingchili.core.context.CoreRuntimeException;
import io.vertx.core.Future;

import com.codingchili.core.listener.CoreHandler;
Expand All @@ -26,6 +27,7 @@ public SimpleBuilderRouter() {
.use(adminRoleRoute, this::adminRoleRoute, ADMIN)
.use(userRoleRoute, this::userRoleRoute, USER)
.use(specialRoute, this::customRouteName)
.use(mappedException, this::throwMappedExcepetion)
.use(multipleUserRoute, this::multipleUserRoute, USER, ADMIN)
.use(customRoleOnRoute, this::customRoleOnRoute, RoleMap.get(CUSTOM_ROLE));
}
Expand All @@ -42,6 +44,10 @@ public Protocol<Request> getProtocol() {
return protocol;
}

public void throwMappedExcepetion(Request request) {
throw new CoreRuntimeException("mapped runtime exception");
}

public void documentedRoute(Request request) {
request.write(documentedRoute);
}
Expand Down
3 changes: 1 addition & 2 deletions core/test/java/com/codingchili/core/storage/QueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void setUp() {

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


@Test
public void testParseQueryString(TestContext test) {
QueryBuilder<Account> builder = new Query<>();
Expand Down

0 comments on commit f16fd44

Please sign in to comment.