Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dea2016

Browse files
committed
throw/catch is much more important than auto-suppression
1 parent 78efc11 commit dea2016

File tree

2 files changed

+21
-27
lines changed
  • src
    • main/java/com/jnape/palatable/lambda/io
    • test/java/com/jnape/palatable/lambda/io

2 files changed

+21
-27
lines changed

src/main/java/com/jnape/palatable/lambda/io/IO.java

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
import java.util.concurrent.Executor;
1818

1919
import static com.jnape.palatable.lambda.adt.Either.left;
20-
import static com.jnape.palatable.lambda.adt.Try.failure;
21-
import static com.jnape.palatable.lambda.adt.Try.trying;
2220
import static com.jnape.palatable.lambda.adt.Unit.UNIT;
2321
import static com.jnape.palatable.lambda.adt.choice.Choice2.a;
2422
import static com.jnape.palatable.lambda.adt.choice.Choice2.b;
@@ -190,28 +188,17 @@ public final IO<A> catchError(Fn1<? super Throwable, ? extends Monad<A, IO<?>>>
190188
return new IO<A>() {
191189
@Override
192190
public A unsafePerformIO() {
193-
return trying(fn0(IO.this::unsafePerformIO))
194-
.recover(t -> trying(fn0(recoveryFn.apply(t).<IO<A>>coerce()::unsafePerformIO))
195-
.fmap(Try::success)
196-
.recover(t2 -> {
197-
t.addSuppressed(t2);
198-
return failure(t);
199-
})
200-
.orThrow());
191+
return Try.trying(IO.this::unsafePerformIO)
192+
.catchError(t -> Try.trying(recoveryFn.apply(t).<IO<A>>coerce()::unsafePerformIO))
193+
.orThrow();
201194
}
202195

203196
@Override
204197
public CompletableFuture<A> unsafePerformAsyncIO(Executor executor) {
205198
return IO.this.unsafePerformAsyncIO(executor)
206-
.thenApply(CompletableFuture::completedFuture)
207-
.exceptionally(t -> recoveryFn.apply(t).<IO<A>>coerce().unsafePerformAsyncIO(executor)
208-
.thenApply(CompletableFuture::completedFuture)
209-
.exceptionally(t2 -> {
210-
t.addSuppressed(t2);
211-
return new CompletableFuture<A>() {{
212-
completeExceptionally(t);
213-
}};
214-
}).thenCompose(f -> f))
199+
.handle((a, t) -> t == null
200+
? completedFuture(a)
201+
: recoveryFn.apply(t).<IO<A>>coerce().unsafePerformAsyncIO(executor))
215202
.thenCompose(f -> f);
216203
}
217204
};

src/test/java/com/jnape/palatable/lambda/io/IOTest.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.ArrayList;
2121
import java.util.List;
2222
import java.util.concurrent.CompletableFuture;
23+
import java.util.concurrent.CompletionException;
2324
import java.util.concurrent.CountDownLatch;
2425
import java.util.concurrent.Executor;
2526
import java.util.concurrent.ExecutorService;
@@ -48,6 +49,7 @@
4849
import static java.util.concurrent.TimeUnit.SECONDS;
4950
import static org.junit.Assert.assertArrayEquals;
5051
import static org.junit.Assert.assertEquals;
52+
import static org.junit.Assert.assertSame;
5153
import static org.junit.Assert.assertTrue;
5254
import static org.junit.Assert.fail;
5355
import static testsupport.Constants.STACK_EXPLODING_NUMBER;
@@ -149,16 +151,21 @@ public void catchError() {
149151
}
150152

151153
@Test
152-
public void catchErrorSuppressesSecondaryThrowable() {
153-
Throwable foo = new UnsupportedOperationException("foo");
154-
Throwable bar = new UnsupportedOperationException("bar");
154+
public void catchAndRethrow() {
155+
IllegalStateException expected = new IllegalStateException("expected");
156+
IO<Object> catchAndRethrow = IO.throwing(expected)
157+
.catchError(IO::throwing);
155158

156159
try {
157-
IO.throwing(foo).catchError(t -> IO.throwing(bar)).unsafePerformIO();
158-
fail("Expected exception to have been thrown, but wasn't.");
159-
} catch (UnsupportedOperationException expected) {
160-
assertEquals(expected, foo);
161-
assertArrayEquals(new Throwable[]{bar}, expected.getSuppressed());
160+
catchAndRethrow.unsafePerformIO();
161+
} catch (Exception actual) {
162+
assertSame(expected, actual);
163+
}
164+
165+
try {
166+
catchAndRethrow.unsafePerformAsyncIO().join();
167+
} catch (CompletionException actual) {
168+
assertEquals(expected, actual.getCause());
162169
}
163170
}
164171

0 commit comments

Comments
 (0)