dmvk commented on a change in pull request #16464:
URL: https://github.com/apache/flink/pull/16464#discussion_r668658040
##########
File path:
flink-core/src/main/java/org/apache/flink/util/concurrent/FutureUtils.java
##########
@@ -1261,6 +1261,43 @@ public static void
assertNoException(CompletableFuture<?> completableFuture) {
handleUncaughtException(completableFuture,
FatalExitExceptionHandler.INSTANCE);
}
+ /**
+ * Checks that the given {@link CompletableFuture} is not completed
exceptionally with the
+ * specified class. If the future is completed exceptionally with the
specific class, then try
+ * to recover using a given exception handler. If the exception does not
match the specified
+ * class, just pass it through to later stages.
+ *
+ * @param completableFuture to assert for a given exception
+ * @param exceptionClass exception class to assert for
+ * @param exceptionHandler to call if the future is completed
exceptionally with the specific
+ * exception
+ * @return completable future, that can recover from a specified exception
+ */
+ public static <T, E extends Throwable> CompletableFuture<T>
handleException(
+ CompletableFuture<T> completableFuture,
+ Class<E> exceptionClass,
+ Function<E, T> exceptionHandler) {
+ final CompletableFuture<T> handledFuture = new CompletableFuture<>();
+ checkNotNull(completableFuture)
+ .whenComplete(
+ (result, throwable) -> {
+ if (throwable == null) {
+ handledFuture.complete(result);
+ } else if
(exceptionClass.isAssignableFrom(throwable.getClass())) {
+ @SuppressWarnings("unchecked")
+ final E exception = (E) throwable;
Review comment:
👍
##########
File path:
flink-core/src/main/java/org/apache/flink/util/concurrent/FutureUtils.java
##########
@@ -1261,6 +1261,43 @@ public static void
assertNoException(CompletableFuture<?> completableFuture) {
handleUncaughtException(completableFuture,
FatalExitExceptionHandler.INSTANCE);
}
+ /**
+ * Checks that the given {@link CompletableFuture} is not completed
exceptionally with the
+ * specified class. If the future is completed exceptionally with the
specific class, then try
+ * to recover using a given exception handler. If the exception does not
match the specified
+ * class, just pass it through to later stages.
+ *
+ * @param completableFuture to assert for a given exception
+ * @param exceptionClass exception class to assert for
+ * @param exceptionHandler to call if the future is completed
exceptionally with the specific
+ * exception
+ * @return completable future, that can recover from a specified exception
+ */
+ public static <T, E extends Throwable> CompletableFuture<T>
handleException(
+ CompletableFuture<T> completableFuture,
+ Class<E> exceptionClass,
+ Function<E, T> exceptionHandler) {
Review comment:
makes sense 👍
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]