Skip to content

Commit 283a777

Browse files
committed
Did change on method executeWithExceptionHandling
1 parent f7a720f commit 283a777

File tree

6 files changed

+44
-23
lines changed

6 files changed

+44
-23
lines changed

src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/AnnotationUtil.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ public static List<String> getScopeAnnotations(PsiClass type, Set<String> scopes
4040
.filter(scopes::contains)
4141
.distinct()
4242
.collect(Collectors.toList()),
43-
Collections::emptyList, // Fallback value in case of exception
44-
e -> LOGGER.log(Level.WARNING, "Error while calling getScopeAnnotations", e)
43+
e -> {
44+
LOGGER.log(Level.WARNING, "Error while calling getScopeAnnotations", e);
45+
return Collections.<String>emptyList();
46+
}
4547
);
4648
}
4749
}

src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/core/java/validators/annotations/AnnotationValidator.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,10 @@ public String validate(String value, AnnotationAttributeRule rule) {
9696
}
9797
return rule.validate(value);
9898
},
99-
() -> null, // Fallback value supplier in case of exception
100-
e -> LOGGER.log(Level.WARNING, e.getLocalizedMessage(), e)
99+
e -> {
100+
LOGGER.log(Level.WARNING, e.getLocalizedMessage(), e);
101+
return null;
102+
}
101103
);
102104
}
103105

src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/internal/core/java/completion/JavaCompletionDefinition.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,21 @@ public final class JavaCompletionDefinition extends BaseKeyedLazyInstance<IJavaC
5050
public boolean isAdaptedForCompletion(JavaCompletionContext context) {
5151
return ExceptionUtil.executeWithExceptionHandling(
5252
() -> getInstance().isAdaptedForCompletion(context),
53-
() -> false, // Fallback value in case of exception
54-
e -> LOGGER.log(Level.WARNING, "Error while calling isAdaptedForCompletion", e)
53+
e -> {
54+
LOGGER.log(Level.WARNING, "Error while calling isAdaptedForCompletion", e);
55+
return false;
56+
}
5557
);
5658
}
5759

5860
@Override
5961
public List<? extends CompletionItem> collectCompletionItems(JavaCompletionContext context) {
6062
return ExceptionUtil.executeWithExceptionHandling(
6163
() -> getInstance().collectCompletionItems(context),
62-
Collections::emptyList, // Fallback value in case of exception
63-
e -> LOGGER.log(Level.WARNING, "Error while calling collectCompletionItems", e)
64+
e -> {
65+
LOGGER.log(Level.WARNING, "Error while calling collectCompletionItems", e);
66+
return Collections.emptyList();
67+
}
6468
);
6569
}
6670

src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/internal/core/java/corrections/JavaDiagnosticsDefinition.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ public final class JavaDiagnosticsDefinition extends BaseKeyedLazyInstance<IJava
5353
public boolean isAdaptedForDiagnostics(JavaDiagnosticsContext context) {
5454
return ExceptionUtil.executeWithExceptionHandling(
5555
() -> getInstance().isAdaptedForDiagnostics(context),
56-
() -> false, // Fallback value in case of exception
57-
e -> LOGGER.log(Level.WARNING, "Error while calling isAdaptedForDiagnostics", e)
56+
e -> {
57+
LOGGER.log(Level.WARNING, "Error while calling isAdaptedForDiagnostics", e);
58+
return false;
59+
}
5860
);
5961
}
6062

@@ -65,8 +67,10 @@ public void beginDiagnostics(JavaDiagnosticsContext context) {
6567
getInstance().beginDiagnostics(context);
6668
return true;
6769
},
68-
() -> null, // Fallback value supplier in case of exception
69-
e -> LOGGER.log(Level.WARNING, "Error while calling beginDiagnostics", e)
70+
e -> {
71+
LOGGER.log(Level.WARNING, "Error while calling beginDiagnostics", e);
72+
return null;
73+
}
7074
);
7175
}
7276

@@ -77,8 +81,10 @@ public List<Diagnostic> collectDiagnostics(JavaDiagnosticsContext context) {
7781
List<Diagnostic> diagnostics = getInstance().collectDiagnostics(context);
7882
return diagnostics != null ? diagnostics : Collections.emptyList();
7983
},
80-
Collections::emptyList,
81-
e -> LOGGER.log(Level.WARNING, "Error while calling collectDiagnostics", e)
84+
e -> {
85+
LOGGER.log(Level.WARNING, "Error while calling collectDiagnostics", e);
86+
return Collections.emptyList();
87+
}
8288
);
8389
}
8490

@@ -90,8 +96,10 @@ public void endDiagnostics(JavaDiagnosticsContext context) {
9096
getInstance().endDiagnostics(context);
9197
return true;
9298
},
93-
() -> null, // Fallback value supplier in case of exception
94-
e -> LOGGER.log(Level.WARNING, "Error while calling endDiagnostics", e)
99+
e -> {
100+
LOGGER.log(Level.WARNING, "Error while calling endDiagnostics", e);
101+
return null;
102+
}
95103
);
96104
}
97105

src/main/java/io/openliberty/tools/intellij/lsp4mp4ij/psi/internal/faulttolerance/java/MicroProfileFaultToleranceASTValidator.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,10 @@ private void validateAsynchronousAnnotation(PsiMethod node, PsiAnnotation annota
157157
PsiType methodReturnType = node.getReturnType();
158158
return methodReturnType.getCanonicalText();
159159
},
160-
null,
161-
e -> LOGGER.log(Level.WARNING, "An error occurred", e)
160+
e -> {
161+
LOGGER.log(Level.WARNING, "An error occurred", e);
162+
return null;
163+
}
162164
);
163165

164166
if ((!isAllowedReturnTypeForAsynchronousAnnotation(methodReturnTypeString))) {

src/main/java/io/openliberty/tools/intellij/util/ExceptionUtil.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import java.util.concurrent.CancellationException;
1616
import java.util.function.Consumer;
17+
import java.util.function.Function;
1718
import java.util.function.Supplier;
1819
import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionResolveContext;
1920
import io.openliberty.tools.intellij.lsp4mp4ij.psi.core.java.corrections.proposal.ChangeCorrectionProposal;
@@ -24,7 +25,7 @@
2425

2526
public class ExceptionUtil {
2627

27-
public static <T> T executeWithExceptionHandling(Supplier<T> action, Supplier<T> fallback, Consumer<Exception> logger) {
28+
public static <T> T executeWithExceptionHandling(Supplier<T> action, Function<Exception, T> fallback) {
2829
try {
2930
return action.get();
3031
} catch (ProcessCanceledException e) {
@@ -34,8 +35,8 @@ public static <T> T executeWithExceptionHandling(Supplier<T> action, Supplier<T>
3435
} catch (IndexNotReadyException | CancellationException e) {
3536
throw e;
3637
} catch (Exception e) {
37-
logger.accept(e);
38-
return fallback.get(); // Return the fallback value in case of failure
38+
// Invoke the fallback function with the exception to generate a safe return value.
39+
return fallback.apply(e);
3940
}
4041
}
4142

@@ -46,8 +47,10 @@ public static void executeWithWorkspaceEditHandling(JavaCodeActionResolveContext
4647
toResolve.setEdit(we);
4748
return true;
4849
},
49-
() -> null, // Fallback value supplier in case of exception
50-
e -> logger.log(Level.WARNING, logMessage, e)
50+
e -> {
51+
logger.log(Level.WARNING, logMessage, e);
52+
return null;
53+
}
5154
);
5255
}
5356
}

0 commit comments

Comments
 (0)