From 3982fd3ba22c2ae71cebafdc3647dbfd347ff770 Mon Sep 17 00:00:00 2001 From: Vladimir Piskarev Date: Wed, 28 Feb 2024 17:38:13 +0300 Subject: [PATCH] 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 /~https://github.com/eclipse-lsp4j/lsp4j/pull/809#issuecomment-1969721107. --- .../lsp4j/jsonrpc/services/GenericEndpoint.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/org.eclipse.lsp4j.jsonrpc/src/main/java/org/eclipse/lsp4j/jsonrpc/services/GenericEndpoint.java b/org.eclipse.lsp4j.jsonrpc/src/main/java/org/eclipse/lsp4j/jsonrpc/services/GenericEndpoint.java index 9bbe16ad9..8cfa43d09 100644 --- a/org.eclipse.lsp4j.jsonrpc/src/main/java/org/eclipse/lsp4j/jsonrpc/services/GenericEndpoint.java +++ b/org.eclipse.lsp4j.jsonrpc/src/main/java/org/eclipse/lsp4j/jsonrpc/services/GenericEndpoint.java @@ -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; @@ -60,14 +59,20 @@ protected void recursiveFindRpcMethods(Object current, Set> visited, Se AnnotationUtil.findRpcMethods(current.getClass(), visited, (methodInfo) -> { @SuppressWarnings("unchecked") Function> 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) 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) { @@ -179,5 +184,4 @@ public void notify(String method, Object parameter) { protected boolean isOptionalMethod(String method) { return method != null && method.startsWith("$/"); } - }