summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/IOExceptionUtil.java4
-rw-r--r--yolean/src/main/java/com/yahoo/yolean/Exceptions.java58
-rw-r--r--yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java37
3 files changed, 99 insertions, 0 deletions
diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/IOExceptionUtil.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/IOExceptionUtil.java
index 24736683845..26ca069aceb 100644
--- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/IOExceptionUtil.java
+++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/IOExceptionUtil.java
@@ -7,6 +7,10 @@ import java.nio.file.NoSuchFileException;
import java.util.Optional;
/**
+ * Utils related to IOException.
+ *
+ * todo: replace much of the below with com.yahoo.yolean.Exceptions::uncheck
+ *
* @author hakonhall
*/
public class IOExceptionUtil {
diff --git a/yolean/src/main/java/com/yahoo/yolean/Exceptions.java b/yolean/src/main/java/com/yahoo/yolean/Exceptions.java
index 83c381e586f..82677a14242 100644
--- a/yolean/src/main/java/com/yahoo/yolean/Exceptions.java
+++ b/yolean/src/main/java/com/yahoo/yolean/Exceptions.java
@@ -1,6 +1,9 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.yolean;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+
/**
* Helper methods for handling exceptions
*
@@ -44,4 +47,59 @@ public class Exceptions {
return message;
}
+ /**
+ * Wraps any IOException thrown from a runnable in an UncheckedIOException.
+ */
+ public static void uncheck(RunnableThrowingIOException runnable) {
+ try {
+ runnable.run();
+ } catch (IOException e) {
+ throw new UncheckedIOException(e);
+ }
+ }
+
+ /**
+ * Wraps any IOException thrown from a runnable in an UncheckedIOException w/message.
+ */
+ public static void uncheck(RunnableThrowingIOException runnable, String format, String... args) {
+ try {
+ runnable.run();
+ } catch (IOException e) {
+ String message = String.format(format, (Object[]) args);
+ throw new UncheckedIOException(message, e);
+ }
+ }
+
+ @FunctionalInterface
+ public interface RunnableThrowingIOException {
+ void run() throws IOException;
+ }
+
+ /**
+ * Wraps any IOException thrown from a supplier in an UncheckedIOException.
+ */
+ public static <T> T uncheck(SupplierThrowingIOException<T> supplier) {
+ try {
+ return supplier.get();
+ } catch (IOException e) {
+ throw new UncheckedIOException(e);
+ }
+ }
+
+ /**
+ * Wraps any IOException thrown from a supplier in an UncheckedIOException w/message.
+ */
+ public static <T> T uncheck(SupplierThrowingIOException<T> supplier, String format, String... args) {
+ try {
+ return supplier.get();
+ } catch (IOException e) {
+ String message = String.format(format, (Object[]) args);
+ throw new UncheckedIOException(message, e);
+ }
+ }
+
+ @FunctionalInterface
+ public interface SupplierThrowingIOException<T> {
+ T get() throws IOException;
+ }
}
diff --git a/yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java b/yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java
index db605609926..31e27fa2675 100644
--- a/yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java
+++ b/yolean/src/test/java/com/yahoo/yolean/ExceptionsTestCase.java
@@ -3,6 +3,9 @@ package com.yahoo.yolean;
import org.junit.Test;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+
import static org.junit.Assert.assertEquals;
/**
@@ -21,4 +24,38 @@ public class ExceptionsTestCase {
assertEquals("Foo",Exceptions.toMessageString(new Exception(new Exception("Foo"))));
}
+ @Test
+ public void testUnchecks() {
+ try {
+ Exceptions.uncheck(this::throwIO);
+ } catch (UncheckedIOException e) {
+ assertEquals("root cause", e.getCause().getMessage());
+ }
+
+ try {
+ Exceptions.uncheck(this::throwIO, "additional %s", "info");
+ } catch (UncheckedIOException e) {
+ assertEquals("additional info", e.getMessage());
+ }
+
+ try {
+ int i = Exceptions.uncheck(this::throwIOWithReturnValue);
+ } catch (UncheckedIOException e) {
+ assertEquals("root cause", e.getCause().getMessage());
+ }
+
+ try {
+ int i = Exceptions.uncheck(this::throwIOWithReturnValue, "additional %s", "info");
+ } catch (UncheckedIOException e) {
+ assertEquals("additional info", e.getMessage());
+ }
+ }
+
+ private void throwIO() throws IOException {
+ throw new IOException("root cause");
+ }
+
+ private int throwIOWithReturnValue() throws IOException {
+ throw new IOException("root cause");
+ }
}