From 4fb4ba3b5185295000afceaadc79f1795863682c Mon Sep 17 00:00:00 2001 From: Jerry Lee <oldratlee@gmail.com> Date: Sat, 17 Aug 2024 19:58:41 +0800 Subject: [PATCH] =?UTF-8?q?refactor(`ExceptionReporter`):=20rename=20metho?= =?UTF-8?q?d=20to=20`reportUncaughtException`=20and=20adjust=20argument=20?= =?UTF-8?q?meaning=20=F0=9F=92=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../foldright/cffu/CompletableFutureUtils.java | 18 +++++++++--------- .../io/foldright/cffu/ExceptionReporter.java | 11 +++++++---- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java b/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java index 698b375df..403466961 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java +++ b/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java @@ -16,7 +16,7 @@ import java.util.function.*; import static io.foldright.cffu.Delayer.atCfDelayerThread; -import static io.foldright.cffu.ExceptionReporter.reportException; +import static io.foldright.cffu.ExceptionReporter.reportUncaughtException; import static java.util.Objects.requireNonNull; import static java.util.concurrent.CompletableFuture.completedFuture; @@ -3839,13 +3839,13 @@ C completeOnTimeout(C cfThis, @Nullable T value, long timeout, TimeUnit unit) { private static <C extends CompletableFuture<?>> C hopExecutorIfAtCfDelayerThread(C cf, Executor executor) { CompletableFuture<Object> ret = newIncompleteFuture(cf); + // use `cf.handle` method(instead of `cf.whenComplete`) and return null + // in order to prevent reporting the handled exception argument of this `action` at subsequent `exceptionally` cf.handle((v, ex) -> { if (!atCfDelayerThread()) completeCf(ret, v, ex); else executor.execute(() -> completeCf(ret, v, ex)); - // use `cf.handle` method(instead of `cf.whenComplete`) and return null - // in order to prevent reporting the handled argument exception in this `action` in subsequent `exceptionally` return null; - }).exceptionally(ex -> reportException("Exception occurred in handle of executor hop", ex)); + }).exceptionally(ex -> reportUncaughtException("handle of executor hop", ex)); return (C) ret; } @@ -3856,7 +3856,7 @@ private static void completeCf(CompletableFuture<Object> cf, Object value, @Null else cf.completeExceptionally(ex); } catch (Throwable t) { if (ex != null) t.addSuppressed(ex); - reportException("Exception occurred in completeCf", t); + reportUncaughtException("completeCf", t); throw t; // rethrow exception, report to caller } } @@ -4018,11 +4018,11 @@ C peek(C cfThis, BiConsumer<? super T, ? super Throwable> action) { requireNonNull(action, "action is null"); // use `cf.handle` method(instead of `cf.whenComplete`) and return null - // in order to prevent reporting the handled argument exception in this `action` in subsequent `exceptionally` + // in order to prevent reporting the handled exception argument of this `action` at subsequent `exceptionally` cfThis.handle((v, ex) -> { action.accept(v, ex); return null; - }).exceptionally(ex -> reportException("Exception occurred in the action of peek", ex)); + }).exceptionally(ex -> reportUncaughtException("the action of peek", ex)); return cfThis; } @@ -4075,11 +4075,11 @@ C peekAsync(C cfThis, BiConsumer<? super T, ? super Throwable> action, Executor requireNonNull(executor, "executor is null"); // use `cf.handleAsync` method(instead of `cf.whenCompleteAsync`) and return null - // in order to prevent reporting the handled argument exception in this `action` in subsequent `exceptionally` + // in order to prevent reporting the handled exception argument of this `action` at subsequent `exceptionally` cfThis.handleAsync((v, ex) -> { action.accept(v, ex); return null; - }, executor).exceptionally(ex -> reportException("Exception occurred in the action of peekAsync", ex)); + }, executor).exceptionally(ex -> reportUncaughtException("the action of peekAsync", ex)); return cfThis; } diff --git a/cffu-core/src/main/java/io/foldright/cffu/ExceptionReporter.java b/cffu-core/src/main/java/io/foldright/cffu/ExceptionReporter.java index 1966f6a51..f1bc0c5f4 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/ExceptionReporter.java +++ b/cffu-core/src/main/java/io/foldright/cffu/ExceptionReporter.java @@ -1,6 +1,7 @@ package io.foldright.cffu; import edu.umd.cs.findbugs.annotations.Nullable; +import org.jetbrains.annotations.Contract; import org.slf4j.spi.LocationAwareLogger; @@ -17,19 +18,21 @@ class ExceptionReporter { private static final LoggerAdapter logger = getLogger(); @Nullable + @Contract("_, _ -> null") @SuppressWarnings("StatementWithEmptyBody") - static <T> T reportException(String msg, Throwable ex) { + static <T> T reportUncaughtException(String where, Throwable ex) { final String fullReport = "full"; final String shortReport = "short"; final String noneReport = "none"; - String report = System.getProperty("cffu.uncaught.exception.report", shortReport); + final String report = System.getProperty("cffu.uncaught.exception.report", shortReport); + final String msgHead = "Uncaught exception occurred at "; if (noneReport.equalsIgnoreCase(report)) { // pass silently when explicitly silenced. } else if (fullReport.equalsIgnoreCase(report)) { - logger.error(msg, ex); + logger.error(msgHead + where, ex); } else { - logger.error(msg + ", " + ex, null); + logger.error(msgHead + where + ", " + ex, null); } return null;