Skip to content

Commit

Permalink
feat: report exception info of peek action, errors should never pass …
Browse files Browse the repository at this point in the history
…silently 💣 👀
  • Loading branch information
oldratlee committed Jun 3, 2024
1 parent 2901c4d commit 2292db3
Showing 1 changed file with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.jetbrains.annotations.Contract;

import javax.annotation.ParametersAreNonnullByDefault;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -1252,8 +1254,8 @@ public static <T, U> CompletableFuture<U> applyToEitherSuccessAsync(
* Peeks the result by executing the given action when given stage completes, returns the given stage.
* <p>
* When the given stage is complete, the given action is invoked with the result (or {@code null} if none)
* and the exception (or {@code null} if none) of given stage as arguments.
* Whether the supplied action throws an exception or not, do <strong>NOT</strong> affect this cffu.
* and the exception (or {@code null} if none) of given stage as arguments. Whether the supplied action
* throws an exception or not, do <strong>NOT</strong> affect the given CompletableFuture.
* <p>
* Unlike method {@link CompletionStage#handle handle} and like method
* {@link CompletionStage#whenComplete(BiConsumer) whenComplete},
Expand All @@ -1269,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);
cf.whenComplete(action).exceptionally(CompletableFutureUtils::reportExceptionInfoOfPeekAction);
return cf;
}

Expand All @@ -1279,8 +1281,8 @@ C peek(C cf, BiConsumer<? super T, ? super Throwable> action) {
* returns the given stage.
* <p>
* When the given stage is complete, the given action is invoked with the result (or {@code null} if none)
* and the exception (or {@code null} if none) of given stage as arguments.
* Whether the supplied action throws an exception or not, do <strong>NOT</strong> affect this cffu.
* and the exception (or {@code null} if none) of given stage as arguments. Whether the supplied action
* throws an exception or not, do <strong>NOT</strong> affect the given CompletableFuture.
* <p>
* Unlike method {@link CompletionStage#handle handle} and like method
* {@link CompletionStage#whenComplete(BiConsumer) whenComplete},
Expand All @@ -1301,8 +1303,8 @@ C peekAsync(C cf, BiConsumer<? super T, ? super Throwable> action) {
* executes the given action using the supplied Executor, returns the given stage.
* <p>
* When the given stage is complete, the given action is invoked with the result (or {@code null} if none)
* and the exception (or {@code null} if none) of given stage as arguments.
* Whether the supplied action throws an exception or not, do <strong>NOT</strong> affect this cffu.
* and the exception (or {@code null} if none) of given stage as arguments. Whether the supplied action
* throws an exception or not, do <strong>NOT</strong> affect the given CompletableFuture.
* <p>
* Unlike method {@link CompletionStage#handle handle} and like method
* {@link CompletionStage#whenComplete(BiConsumer) whenComplete},
Expand All @@ -1319,10 +1321,25 @@ 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);
cf.whenCompleteAsync(action, executor).exceptionally(CompletableFutureUtils::reportExceptionInfoOfPeekAction);
return cf;
}

@Nullable
@SuppressWarnings("SameReturnValue")
private static <T> T reportExceptionInfoOfPeekAction(@Nullable Throwable ex) {
if (ex == null) return null;

StringWriter sw = new StringWriter(4096);
PrintWriter writer = new PrintWriter(sw);

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

System.err.println(sw);
return null;
}

////////////////////////////////////////////////////////////////////////////////
//# Backport CF static methods + related enhanced methods
// compatibility for low Java versions
Expand Down

0 comments on commit 2292db3

Please sign in to comment.