You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by ch...@apache.org on 2018/03/23 18:12:04 UTC
[2/7] flink git commit: [hotfix][utils] Add
ExceptionUtils#findThrowable with predicate
[hotfix][utils] Add ExceptionUtils#findThrowable with predicate
Project: http://git-wip-us.apache.org/repos/asf/flink/repo
Commit: http://git-wip-us.apache.org/repos/asf/flink/commit/0ce85746
Tree: http://git-wip-us.apache.org/repos/asf/flink/tree/0ce85746
Diff: http://git-wip-us.apache.org/repos/asf/flink/diff/0ce85746
Branch: refs/heads/master
Commit: 0ce8574603aac784da30a354f608ff939e09ef58
Parents: 0c56e19
Author: zentol <ch...@apache.org>
Authored: Tue Mar 20 11:38:24 2018 +0100
Committer: zentol <ch...@apache.org>
Committed: Fri Mar 23 19:11:49 2018 +0100
----------------------------------------------------------------------
.../org/apache/flink/util/ExceptionUtils.java | 25 ++++++++++++++++++++
1 file changed, 25 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/flink/blob/0ce85746/flink-core/src/main/java/org/apache/flink/util/ExceptionUtils.java
----------------------------------------------------------------------
diff --git a/flink-core/src/main/java/org/apache/flink/util/ExceptionUtils.java b/flink-core/src/main/java/org/apache/flink/util/ExceptionUtils.java
index 6af16fc..459648f 100644
--- a/flink-core/src/main/java/org/apache/flink/util/ExceptionUtils.java
+++ b/flink-core/src/main/java/org/apache/flink/util/ExceptionUtils.java
@@ -35,6 +35,7 @@ import java.io.StringWriter;
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;
@@ -326,6 +327,30 @@ public final class ExceptionUtils {
}
/**
+ * 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.
*
* @param throwable the throwable chain to check.