aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/mammoth/internal/util/PassThroughException.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/zwobble/mammoth/internal/util/PassThroughException.java')
-rw-r--r--src/main/java/org/zwobble/mammoth/internal/util/PassThroughException.java32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/main/java/org/zwobble/mammoth/internal/util/PassThroughException.java b/src/main/java/org/zwobble/mammoth/internal/util/PassThroughException.java
new file mode 100644
index 0000000..155490f
--- /dev/null
+++ b/src/main/java/org/zwobble/mammoth/internal/util/PassThroughException.java
@@ -0,0 +1,32 @@
+package org.zwobble.mammoth.internal.util;
+
+import java.io.IOException;
+
+public class PassThroughException extends RuntimeException {
+ public interface SupplierWithException<T, E extends Throwable> {
+ public T get() throws E;
+ }
+
+ public static <T> T wrap(SupplierWithException<T, IOException> supplier) {
+ try {
+ return supplier.get();
+ } catch (IOException exception) {
+ throw new PassThroughException(exception);
+ }
+ }
+
+ public static <T> T unwrap(SupplierWithException<T, IOException> supplier) throws IOException {
+ try {
+ return supplier.get();
+ } catch (PassThroughException exception) {
+ throw exception.exception;
+ }
+ }
+
+ private final IOException exception;
+
+ public PassThroughException(IOException exception) {
+ super(exception);
+ this.exception = exception;
+ }
+}