Skip to content

Commit

Permalink
refactor: add addSuppressed for reportUncaughtException 💣
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Dec 13, 2024
1 parent 7614edb commit 8199d0e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3061,26 +3061,22 @@ 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 screenExecutor(executor).execute(() -> completeCf(ret, v, ex));
return null;
}).exceptionally(ex -> reportUncaughtException("handle of executor hop", ex));
cf.whenComplete((v, ex) -> {
try {
if (!atCfDelayerThread()) completeCf(ret, v, ex);
else screenExecutor(executor).execute(() -> completeCf(ret, v, ex));
} catch (Throwable e) {
if (ex != null) e.addSuppressed(ex);
reportUncaughtException("handle of executor hop", e);
}
});

return (C) ret;
}

private static void completeCf(CompletableFuture<Object> cf, Object value, @Nullable Throwable ex) {
try {
if (ex == null) cf.complete(value);
else cf.completeExceptionally(ex);
} catch (Throwable t) {
if (ex != null) t.addSuppressed(ex);
reportUncaughtException("completeCf", t);
throw t; // rethrow exception, report to caller
}
if (ex == null) cf.complete(value);
else cf.completeExceptionally(ex);
}

// endregion
Expand Down Expand Up @@ -3256,12 +3252,14 @@ C peek(C cfThis, BiConsumer<? super T, ? super Throwable> action) {
requireNonNull(cfThis, "cfThis is null");
requireNonNull(action, "action is null");

// 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`
cfThis.handle((v, ex) -> {
action.accept(v, ex);
return null;
}).exceptionally(ex -> reportUncaughtException("the action of peek", ex));
cfThis.whenComplete((v, ex) -> {
try {
action.accept(v, ex);
} catch (Throwable e) {
if (ex != null) e.addSuppressed(ex);
reportUncaughtException("the action of peek", e);
}
});
return cfThis;
}

Expand Down Expand Up @@ -3322,12 +3320,14 @@ C peekAsync(C cfThis, BiConsumer<? super T, ? super Throwable> action, Executor
requireNonNull(action, "action is null");
requireNonNull(executor, "executor is null");

// use `cf.handleAsync` method(instead of `cf.whenCompleteAsync`) and return null 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 -> reportUncaughtException("the action of peekAsync", ex));
cfThis.whenCompleteAsync((v, ex) -> {
try {
action.accept(v, ex);
} catch (Throwable e) {
if (ex != null) e.addSuppressed(ex);
reportUncaughtException("handle of executor hop", e);
}
}, executor);
return cfThis;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.foldright.cffu;

import edu.umd.cs.findbugs.annotations.Nullable;
import org.jetbrains.annotations.Contract;
import org.slf4j.spi.LocationAwareLogger;


Expand All @@ -17,10 +16,8 @@ final class ExceptionReporter {

private static final LoggerAdapter logger = getLogger();

@Nullable
@Contract("_, _ -> null")
@SuppressWarnings({"StatementWithEmptyBody", "SameReturnValue"})
static <T> T reportUncaughtException(String where, Throwable ex) {
@SuppressWarnings("StatementWithEmptyBody")
static void reportUncaughtException(String where, Throwable ex) {
final String fullReport = "full";
final String shortReport = "short";
final String noneReport = "none";
Expand All @@ -34,8 +31,6 @@ static <T> T reportUncaughtException(String where, Throwable ex) {
} else {
logger.error(msgHead + where, ex);
}

return null;
}

private static LoggerAdapter getLogger() {
Expand Down

0 comments on commit 8199d0e

Please sign in to comment.