diff options
author | Valerij Fredriksen <valerijf@oath.com> | 2018-12-07 15:33:17 +0100 |
---|---|---|
committer | Valerij Fredriksen <valerijf@oath.com> | 2018-12-07 15:33:17 +0100 |
commit | 5f848c88c26cb41c01e3338a46f383a61d89a969 (patch) | |
tree | 3fbcc6acfe996edb0a1241c29b5d435a5b0ee70c /yolean/src/main | |
parent | ce1b30a518593155aad5e233ef40f068e23a93de (diff) |
Add Exceptions.findCause()
Diffstat (limited to 'yolean/src/main')
-rw-r--r-- | yolean/src/main/java/com/yahoo/yolean/Exceptions.java | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/yolean/src/main/java/com/yahoo/yolean/Exceptions.java b/yolean/src/main/java/com/yahoo/yolean/Exceptions.java index fa3eb412016..063ba70c75d 100644 --- a/yolean/src/main/java/com/yahoo/yolean/Exceptions.java +++ b/yolean/src/main/java/com/yahoo/yolean/Exceptions.java @@ -3,9 +3,7 @@ package com.yahoo.yolean; import java.io.IOException; import java.io.UncheckedIOException; -import java.nio.file.NoSuchFileException; import java.util.Optional; -import java.util.function.Supplier; /** * Helper methods for handling exceptions @@ -51,6 +49,17 @@ public class Exceptions { } /** + * Returns the first cause or the given throwable that is an instance of {@code clazz} + */ + public static <T extends Throwable> Optional<T> findCause(Throwable t, Class<T> clazz) { + for (; t != null; t = t.getCause()) { + if (clazz.isInstance(t)) + return Optional.of(clazz.cast(t)); + } + return Optional.empty(); + } + + /** * Wraps any IOException thrown from a runnable in an UncheckedIOException. */ public static void uncheck(RunnableThrowingIOException runnable) { |