diff --git a/libs/natls/src/main/java/org/amshove/natls/SafeWrap.java b/libs/natls/src/main/java/org/amshove/natls/SafeWrap.java new file mode 100644 index 000000000..dfa39eeb2 --- /dev/null +++ b/libs/natls/src/main/java/org/amshove/natls/SafeWrap.java @@ -0,0 +1,40 @@ +package org.amshove.natls; + +import java.util.concurrent.CompletableFuture; +import java.util.function.Supplier; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class SafeWrap +{ + private static final Logger log = Logger.getAnonymousLogger(); + + private SafeWrap() + {} + + public static CompletableFuture wrapSafe(Supplier> function) + { + return function.get().handle((r, e) -> + { + if (e != null) + { + log.log(Level.SEVERE, "Uncaught exception", e); + return null; + } + + return r; + }); + } + + public static void wrapSafe(Runnable runnable) + { + try + { + runnable.run(); + } + catch (Exception e) + { + log.log(Level.SEVERE, "Uncaught exception", e); + } + } +} diff --git a/libs/natls/src/main/java/org/amshove/natls/languageserver/NaturalDocumentService.java b/libs/natls/src/main/java/org/amshove/natls/languageserver/NaturalDocumentService.java index 8bb6b9c9f..b707bffe3 100644 --- a/libs/natls/src/main/java/org/amshove/natls/languageserver/NaturalDocumentService.java +++ b/libs/natls/src/main/java/org/amshove/natls/languageserver/NaturalDocumentService.java @@ -7,7 +7,8 @@ import java.util.List; import java.util.concurrent.CompletableFuture; -import java.util.function.Supplier; + +import static org.amshove.natls.SafeWrap.wrapSafe; @SuppressWarnings("deprecation") public class NaturalDocumentService implements TextDocumentService @@ -167,30 +168,4 @@ public void setLanguageService(NaturalLanguageService languageService) { this.languageService = languageService; } - - private CompletableFuture wrapSafe(Supplier> function) - { - return function.get().handle((r, e) -> - { - if (e != null) - { - // TODO: log exception - return null; - } - - return r; - }); - } - - private void wrapSafe(Runnable runnable) - { - try - { - runnable.run(); - } - catch (Exception e) - { - // TODO: log exception - } - } } diff --git a/libs/natls/src/main/java/org/amshove/natls/languageserver/NaturalLanguageServer.java b/libs/natls/src/main/java/org/amshove/natls/languageserver/NaturalLanguageServer.java index 44e194e21..6ed25b058 100644 --- a/libs/natls/src/main/java/org/amshove/natls/languageserver/NaturalLanguageServer.java +++ b/libs/natls/src/main/java/org/amshove/natls/languageserver/NaturalLanguageServer.java @@ -27,6 +27,8 @@ import java.util.logging.Level; import java.util.logging.Logger; +import static org.amshove.natls.SafeWrap.wrapSafe; + public class NaturalLanguageServer implements LanguageServer, LanguageClientAware { private static final Logger log = Logger.getAnonymousLogger(); @@ -257,35 +259,35 @@ public CompletableFuture reparseReferences(Object params) languageService.parseFileReferencesAsync(); } - return CompletableFuture.completedFuture(null); + return wrapSafe(() -> CompletableFuture.completedFuture(null)); } @JsonRequest @SuppressWarnings("unused") public CompletableFuture referableFileExists(ReferableFileExistsParams params) { - return CompletableFuture.completedFuture(new ReferableFileExistsResponse(languageService.findReferableName(params.getLibrary(), params.getReferableName()) != null)); + return wrapSafe(() -> CompletableFuture.completedFuture(new ReferableFileExistsResponse(languageService.findReferableName(params.getLibrary(), params.getReferableName()) != null))); } @JsonRequest @SuppressWarnings("unused") public CompletableFuture calledModules(CalledModulesParams params) { - return CompletableFuture.supplyAsync(() -> languageService.getCalledModules(params.getIdentifier())); + return wrapSafe(() -> CompletableFuture.supplyAsync(() -> languageService.getCalledModules(params.getIdentifier()))); } @JsonRequest @SuppressWarnings("unused") public CompletableFuture inputStructure(InputStructureParams params) { - return CompletableFuture.supplyAsync(() -> languageService.getInputStructure(params)); + return wrapSafe(() -> CompletableFuture.supplyAsync(() -> languageService.getInputStructure(params))); } @JsonRequest @SuppressWarnings("unused") public CompletableFuture findConstants(FindConstantsParams params) { - return CompletableFuture.supplyAsync(() -> languageService.findConstants(params)); + return wrapSafe(() -> CompletableFuture.supplyAsync(() -> languageService.findConstants(params))); } @Override