Skip to content

Commit

Permalink
Treat checked exceptions from jsonrpc methods as unexpected
Browse files Browse the repository at this point in the history
Checked exceptions should not be thrown from annotated jsonrpc methods.
Such exceptions can be treated as a programming error, and signaled via
an IllegalStateException in the GenericEndpoint.

An inaccessible jsonrpc method can be handled in a similar way.

For detailed discussion and context, see
#809 (comment).
  • Loading branch information
pisv authored and jonahgraham committed Feb 29, 2024
1 parent b76bb7e commit c1d4cd3
Showing 1 changed file with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -60,14 +59,20 @@ protected void recursiveFindRpcMethods(Object current, Set<Class<?>> visited, Se
AnnotationUtil.findRpcMethods(current.getClass(), visited, (methodInfo) -> {
@SuppressWarnings("unchecked")
Function<Object, CompletableFuture<Object>> handler = (arg) -> {
Method method = methodInfo.method;
Object[] arguments = this.getArguments(method, arg);
try {
Method method = methodInfo.method;
Object[] arguments = this.getArguments(method, arg);
return (CompletableFuture<Object>) method.invoke(current, arguments);
} catch (InvocationTargetException e) {
throw new CompletionException(e.getCause());
Throwable cause = e.getCause();
if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
} else if (cause instanceof Error) {
throw (Error) cause;
}
throw new IllegalStateException("An unexpected exception occurred while executing jsonrpc method " + method, cause);
} catch (IllegalAccessException e) {
throw new CompletionException(e);
throw new IllegalStateException("Inaccessible jsonrpc method: " + method, e);
}
};
if (methodHandlers.put(methodInfo.name, handler) != null) {
Expand Down Expand Up @@ -179,5 +184,4 @@ public void notify(String method, Object parameter) {
protected boolean isOptionalMethod(String method) {
return method != null && method.startsWith("$/");
}

}

0 comments on commit c1d4cd3

Please sign in to comment.