Skip to content

Commit

Permalink
feat: report exception info of hop handle, errors should never pass s…
Browse files Browse the repository at this point in the history
…ilently 💣 👀
  • Loading branch information
oldratlee committed Jun 4, 2024
1 parent 5375f54 commit 6811105
Showing 1 changed file with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1271,7 +1271,7 @@ C peek(C cf, BiConsumer<? super T, ? super Throwable> action) {
requireNonNull(cf, "cf is null");
requireNonNull(action, "action is null");

cf.whenComplete(action).exceptionally(CompletableFutureUtils::reportExceptionInfoOfPeekAction);
cf.whenComplete(action).exceptionally(ex -> reportException("Exception occurred in the action of peek:", ex));
return cf;
}

Expand Down Expand Up @@ -1321,16 +1321,17 @@ C peekAsync(C cf, BiConsumer<? super T, ? super Throwable> action, Executor exec
requireNonNull(action, "action is null");
requireNonNull(executor, "executor is null");

cf.whenCompleteAsync(action, executor).exceptionally(CompletableFutureUtils::reportExceptionInfoOfPeekAction);
cf.whenCompleteAsync(action, executor).exceptionally(ex ->
reportException("Exception occurred in the action of peekAsync:", ex));
return cf;
}

@Nullable
private static <T> T reportExceptionInfoOfPeekAction(Throwable ex) {
private static <T> T reportException(String msg, Throwable ex) {
StringWriter sw = new StringWriter(4096);
PrintWriter writer = new PrintWriter(sw);

writer.println("Exception occurred in the action of peek:");
writer.println(msg);
ex.printStackTrace(writer);

System.err.println(sw);
Expand Down Expand Up @@ -1642,14 +1643,20 @@ private static <C extends CompletableFuture<?>> C hopExecutorIfAtCfDelayerThread
if (!atCfDelayerThread()) completeCf(ret, v, ex);
else delayedExecutor(0, TimeUnit.SECONDS, asyncExecutor)
.execute(() -> completeCf(ret, v, ex));
});
}).exceptionally(ex -> reportException("Exception occurred in the input cf whenComplete of hop executor:", ex));

return (C) ret;
}

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

//# Advanced methods of CompletionStage
Expand Down

0 comments on commit 6811105

Please sign in to comment.