Skip to content

Commit e3d8618

Browse files
committed
Error Handler: on CompletionException uses getCause()
fix #2452
1 parent 32abff2 commit e3d8618

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

jooby/src/main/java/io/jooby/internal/handler/CompletionStageHandler.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
import io.jooby.Route;
1010

1111
import javax.annotation.Nonnull;
12+
13+
import java.util.Optional;
1214
import java.util.concurrent.CompletableFuture;
15+
import java.util.concurrent.CompletionException;
1316
import java.util.concurrent.CompletionStage;
1417

1518
public class CompletionStageHandler implements LinkedHandler {
@@ -29,7 +32,11 @@ public CompletionStageHandler(Route.Handler next) {
2932
return ((CompletionStage) result).whenComplete((value, x) -> {
3033
try {
3134
if (x != null) {
32-
ctx.sendError((Throwable) x);
35+
Throwable exception = (Throwable) x;
36+
if (exception instanceof CompletionException) {
37+
exception = Optional.ofNullable(exception.getCause()).orElse(exception);
38+
}
39+
ctx.sendError(exception);
3340
} else {
3441
ctx.render(value);
3542
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package io.jooby.i2452;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.concurrent.CompletableFuture;
6+
7+
import org.jetbrains.annotations.NotNull;
8+
9+
import io.jooby.Context;
10+
import io.jooby.ErrorHandler;
11+
import io.jooby.StatusCode;
12+
import io.jooby.WebClient;
13+
import io.jooby.exception.StatusCodeException;
14+
import io.jooby.junit.ServerTest;
15+
import io.jooby.junit.ServerTestRunner;
16+
17+
public class Issue2452 {
18+
@ServerTest
19+
public void shouldHandleCompletableFutureError(ServerTestRunner runner) {
20+
runner.define(app -> {
21+
app.get("/2452", ctx -> CompletableFuture.supplyAsync(
22+
() -> {
23+
throw new StatusCodeException(StatusCode.NOT_FOUND);
24+
}
25+
));
26+
27+
app.error((ctx, cause, code) -> {
28+
ctx.setResponseCode(code);
29+
ctx.send(cause.getClass().getSimpleName());
30+
});
31+
}).ready((WebClient http) -> {
32+
33+
http
34+
.get("/2452", rsp -> {
35+
assertEquals("StatusCodeException",
36+
rsp.body().string());
37+
assertEquals(StatusCode.NOT_FOUND.value(),
38+
rsp.code());
39+
});
40+
});
41+
}
42+
}

0 commit comments

Comments
 (0)