Skip to content

Commit c1d4cd3

Browse files
pisvjonahgraham
authored andcommitted
Treat checked exceptions from jsonrpc methods as unexpected
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).
1 parent b76bb7e commit c1d4cd3

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

org.eclipse.lsp4j.jsonrpc/src/main/java/org/eclipse/lsp4j/jsonrpc/services/GenericEndpoint.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.List;
2222
import java.util.Set;
2323
import java.util.concurrent.CompletableFuture;
24-
import java.util.concurrent.CompletionException;
2524
import java.util.function.Function;
2625
import java.util.logging.Level;
2726
import java.util.logging.Logger;
@@ -60,14 +59,20 @@ protected void recursiveFindRpcMethods(Object current, Set<Class<?>> visited, Se
6059
AnnotationUtil.findRpcMethods(current.getClass(), visited, (methodInfo) -> {
6160
@SuppressWarnings("unchecked")
6261
Function<Object, CompletableFuture<Object>> handler = (arg) -> {
62+
Method method = methodInfo.method;
63+
Object[] arguments = this.getArguments(method, arg);
6364
try {
64-
Method method = methodInfo.method;
65-
Object[] arguments = this.getArguments(method, arg);
6665
return (CompletableFuture<Object>) method.invoke(current, arguments);
6766
} catch (InvocationTargetException e) {
68-
throw new CompletionException(e.getCause());
67+
Throwable cause = e.getCause();
68+
if (cause instanceof RuntimeException) {
69+
throw (RuntimeException) cause;
70+
} else if (cause instanceof Error) {
71+
throw (Error) cause;
72+
}
73+
throw new IllegalStateException("An unexpected exception occurred while executing jsonrpc method " + method, cause);
6974
} catch (IllegalAccessException e) {
70-
throw new CompletionException(e);
75+
throw new IllegalStateException("Inaccessible jsonrpc method: " + method, e);
7176
}
7277
};
7378
if (methodHandlers.put(methodInfo.name, handler) != null) {
@@ -179,5 +184,4 @@ public void notify(String method, Object parameter) {
179184
protected boolean isOptionalMethod(String method) {
180185
return method != null && method.startsWith("$/");
181186
}
182-
183187
}

0 commit comments

Comments
 (0)