Skip to content

Commit

Permalink
[hotfix][utils] Add ExceptionUtils#findThrowable with predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
zentol committed Mar 23, 2018
1 parent 0c56e19 commit 0ce8574
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions flink-core/src/main/java/org/apache/flink/util/ExceptionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.Optional;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;
import java.util.function.Predicate;

import static org.apache.flink.util.Preconditions.checkNotNull;

Expand Down Expand Up @@ -325,6 +326,30 @@ public static <T extends Throwable> Optional<T> findThrowable(Throwable throwabl
return Optional.empty();
}

/**
* Checks whether a throwable chain contains an exception matching a predicate and returns it.
*
* @param throwable the throwable chain to check.
* @param predicate the predicate of the exception to search for in the chain.
* @return Optional throwable of the requested type if available, otherwise empty
*/
public static Optional<Throwable> findThrowable(Throwable throwable, Predicate<Throwable> predicate) {
if (throwable == null || predicate == null) {
return Optional.empty();
}

Throwable t = throwable;
while (t != null) {
if (predicate.test(t)) {
return Optional.of(t);
} else {
t = t.getCause();
}
}

return Optional.empty();
}

/**
* Checks whether a throwable chain contains a specific error message and returns the corresponding throwable.
*
Expand Down

0 comments on commit 0ce8574

Please sign in to comment.