File tree Expand file tree Collapse file tree 2 files changed +50
-1
lines changed
jooby/src/main/java/io/jooby/internal/handler
tests/src/test/java/io/jooby/i2452 Expand file tree Collapse file tree 2 files changed +50
-1
lines changed Original file line number Diff line number Diff line change 99import io .jooby .Route ;
1010
1111import javax .annotation .Nonnull ;
12+
13+ import java .util .Optional ;
1214import java .util .concurrent .CompletableFuture ;
15+ import java .util .concurrent .CompletionException ;
1316import java .util .concurrent .CompletionStage ;
1417
1518public 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 }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments