Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log ignored exceptions on endpoints #501

Merged
merged 2 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions libs/natls/src/main/java/org/amshove/natls/SafeWrap.java
Original file line number Diff line number Diff line change
@@ -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 <R> CompletableFuture<R> wrapSafe(Supplier<CompletableFuture<R>> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -167,30 +168,4 @@ public void setLanguageService(NaturalLanguageService languageService)
{
this.languageService = languageService;
}

private <R> CompletableFuture<R> wrapSafe(Supplier<CompletableFuture<R>> 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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -257,35 +259,35 @@ public CompletableFuture<Void> reparseReferences(Object params)
languageService.parseFileReferencesAsync();
}

return CompletableFuture.completedFuture(null);
return wrapSafe(() -> CompletableFuture.completedFuture(null));
}

@JsonRequest
@SuppressWarnings("unused")
public CompletableFuture<ReferableFileExistsResponse> 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<CalledModulesResponse> calledModules(CalledModulesParams params)
{
return CompletableFuture.supplyAsync(() -> languageService.getCalledModules(params.getIdentifier()));
return wrapSafe(() -> CompletableFuture.supplyAsync(() -> languageService.getCalledModules(params.getIdentifier())));
}

@JsonRequest
@SuppressWarnings("unused")
public CompletableFuture<InputStructureResponse> inputStructure(InputStructureParams params)
{
return CompletableFuture.supplyAsync(() -> languageService.getInputStructure(params));
return wrapSafe(() -> CompletableFuture.supplyAsync(() -> languageService.getInputStructure(params)));
}

@JsonRequest
@SuppressWarnings("unused")
public CompletableFuture<FindConstantsResponse> findConstants(FindConstantsParams params)
{
return CompletableFuture.supplyAsync(() -> languageService.findConstants(params));
return wrapSafe(() -> CompletableFuture.supplyAsync(() -> languageService.findConstants(params)));
}

@Override
Expand Down