aboutsummaryrefslogtreecommitdiffstats
path: root/yolean
diff options
context:
space:
mode:
authorValerij Fredriksen <valerijf@oath.com>2018-12-07 15:33:17 +0100
committerValerij Fredriksen <valerijf@oath.com>2018-12-07 15:33:17 +0100
commit5f848c88c26cb41c01e3338a46f383a61d89a969 (patch)
tree3fbcc6acfe996edb0a1241c29b5d435a5b0ee70c /yolean
parentce1b30a518593155aad5e233ef40f068e23a93de (diff)
Add Exceptions.findCause()
Diffstat (limited to 'yolean')
-rw-r--r--yolean/src/main/java/com/yahoo/yolean/Exceptions.java13
-rw-r--r--yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java14
2 files changed, 25 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) {
diff --git a/yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java b/yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java
index 390018efdf7..cd06d14bb55 100644
--- a/yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java
+++ b/yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java
@@ -6,6 +6,7 @@ import org.junit.Test;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.NoSuchFileException;
+import java.util.Optional;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@@ -17,6 +18,19 @@ import static org.junit.Assert.assertNull;
public class ExceptionsTestCase {
@Test
+ public void testFindCause() {
+ IllegalArgumentException e1 = new IllegalArgumentException();
+ IllegalStateException e2 = new IllegalStateException(e1);
+ RuntimeException e3 = new RuntimeException(e2);
+
+ assertEquals(Optional.of(e3), Exceptions.findCause(e3, RuntimeException.class));
+ assertEquals(Optional.of(e1), Exceptions.findCause(e3, IllegalArgumentException.class));
+ assertEquals(Optional.empty(), Exceptions.findCause(e3, NumberFormatException.class));
+
+ assertEquals(Optional.of(e2), Exceptions.findCause(e2, RuntimeException.class));
+ }
+
+ @Test
public void testToMessageStrings() {
assertEquals("Blah",Exceptions.toMessageString(new Exception("Blah")));
assertEquals("Blah", Exceptions.toMessageString(new Exception(new Exception("Blah"))));