Skip to content

Commit

Permalink
refactor(ExceptionReporter): rename method to `reportUncaughtExcept…
Browse files Browse the repository at this point in the history
…ion` and adjust argument meaning 💣
  • Loading branch information
oldratlee committed Aug 23, 2024
1 parent 88a6221 commit 4fb4ba3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
11 changes: 7 additions & 4 deletions cffu-core/src/main/java/io/foldright/cffu/ExceptionReporter.java
Original file line number Diff line number Diff line change
@@ -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;


Expand All @@ -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;
Expand Down

0 comments on commit 4fb4ba3

Please sign in to comment.