summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--application/src/main/java/com/yahoo/application/Application.java6
-rw-r--r--yolean/src/main/java/com/yahoo/yolean/Exceptions.java13
-rw-r--r--yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java14
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"))));