Skip to content

Commit 08ba192

Browse files
committed
IOException becomes UncheckedIOException
1 parent bc9dfef commit 08ba192

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/main/java/no/digipost/DiggExceptions.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import no.digipost.function.ThrowingRunnable;
2323
import no.digipost.function.ThrowingSupplier;
2424

25+
import java.io.IOException;
26+
import java.io.UncheckedIOException;
2527
import java.util.function.Consumer;
2628
import java.util.function.Function;
2729
import java.util.stream.Stream;
@@ -102,7 +104,11 @@ public static <X extends Throwable> RuntimeException asUnchecked(X t, Function<?
102104
* and the given exception as its {@link Throwable#getCause() cause}
103105
*/
104106
public static RuntimeException asUnchecked(Throwable t, String message) {
105-
return new RuntimeException(message, t);
107+
if (t instanceof IOException) {
108+
return new UncheckedIOException(message, (IOException) t);
109+
} else {
110+
return new RuntimeException(message, t);
111+
}
106112
}
107113

108114
/**

src/test/java/no/digipost/DiggExceptionsTest.java

+7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.junit.jupiter.api.Test;
2121

2222
import java.io.IOException;
23+
import java.io.UncheckedIOException;
2324
import java.util.List;
2425
import java.util.Optional;
2526
import java.util.function.Consumer;
@@ -124,6 +125,12 @@ public MyCheckedException() {
124125
() -> assertThat(uncheckedException, where(Exception::getMessage, containsString("DiggExceptionsTest.AsUnchecked.MyCheckedException"))),
125126
() -> assertThat(uncheckedException, where(Exception::getMessage, containsString("Who in their right mind"))));
126127
}
128+
129+
@Test
130+
void ioExceptionAsUncheckedIOException() {
131+
assertThat(new IOException("error"), where(DiggExceptions::asUnchecked, instanceOf(UncheckedIOException.class)));
132+
}
133+
127134
}
128135

129136
}

0 commit comments

Comments
 (0)