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 | |
parent | ce1b30a518593155aad5e233ef40f068e23a93de (diff) |
Add Exceptions.findCause()
3 files changed, 29 insertions, 4 deletions
diff --git a/application/src/main/java/com/yahoo/application/Application.java b/application/src/main/java/com/yahoo/application/Application.java index 88140873b7b..64ced48edd3 100644 --- a/application/src/main/java/com/yahoo/application/Application.java +++ b/application/src/main/java/com/yahoo/application/Application.java @@ -20,6 +20,7 @@ import com.yahoo.search.rendering.Renderer; import com.yahoo.text.StringUtilities; import com.yahoo.text.Utf8; import com.yahoo.vespa.model.VespaModel; +import com.yahoo.yolean.Exceptions; import org.xml.sax.SAXException; import java.io.File; @@ -315,8 +316,9 @@ public final class Application implements AutoCloseable { break; } catch (Error e) { // the container thinks this is really serious, in this case is it not in the cause is a BindException // catch bind error and reset container - if (e.getCause() != null && e.getCause().getCause() != null && e.getCause().getCause() instanceof BindException) { - exception = (Exception) e.getCause().getCause(); + Optional<BindException> bindException = Exceptions.findCause(e, BindException.class); + if (bindException.isPresent()) { + exception = bindException.get(); com.yahoo.container.Container.resetInstance(); // this is needed to be able to recreate the container from config again } else { throw new Exception(e.getCause()); 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")))); |