diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c6591f..5f65786 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ Proteus Changelog. ## Unreleased ### No issue +**Ensure worker is used for async dispatch.** + + +[6482aac600edd62](https://github.com/noboomu/proteus/commit/6482aac600edd62) Joshua Bauer *2020-09-15 17:16:40* + **Send** diff --git a/proteus-core/src/main/java/io/sinistral/proteus/server/handlers/HandlerGenerator.java b/proteus-core/src/main/java/io/sinistral/proteus/server/handlers/HandlerGenerator.java index 0ef2ec4..062f89a 100644 --- a/proteus-core/src/main/java/io/sinistral/proteus/server/handlers/HandlerGenerator.java +++ b/proteus-core/src/main/java/io/sinistral/proteus/server/handlers/HandlerGenerator.java @@ -677,7 +677,7 @@ protected void addClassMethodHandlers(TypeSpec.Builder typeBuilder, Class cla if (m.getReturnType().equals(ServerResponse.class)) { - methodBuilder.addStatement("$L.send(this,$L)", "response", "exchange"); + methodBuilder.addStatement("$L.send($L)", "response", "exchange"); } else if ((m.getGenericReturnType().toString().contains("java.util.concurrent.CompletionStage") && m.getGenericReturnType().toString().contains("ServerResponse")) || (m.getGenericReturnType().toString().contains("java.util.concurrent.CompletableFuture") && m.getGenericReturnType().toString().contains("ServerResponse"))) @@ -687,13 +687,20 @@ protected void addClassMethodHandlers(TypeSpec.Builder typeBuilder, Class cla methodBuilder.beginControlFlow("", ""); methodBuilder.addCode( - "$L.thenAccept( r -> r.send($L) )\n\t.exceptionally( ex -> ", - "response", "exchange"); - methodBuilder.beginControlFlow("", ""); - methodBuilder.addCode("\t\tthrow new java.util.concurrent.CompletionException(ex);\n\t"); - methodBuilder.endControlFlow(")", ""); + "$L.whenComplete( (r,ex) -> ", + "response"); + methodBuilder.beginControlFlow("", ""); + + methodBuilder.beginControlFlow("if(ex != null)"); + methodBuilder.addCode("\t\texchange.putAttachment(io.undertow.server.handlers.ExceptionHandler.THROWABLE, ex);"); + methodBuilder.addCode("\t\texchange.setResponseCode(500);\n\t"); + methodBuilder.addCode("\t\texchange.endExchange();\n\t"); + methodBuilder.nextControlFlow("else"); + methodBuilder.addCode("\t\tr.send($L);","exchange"); + methodBuilder.endControlFlow(); + methodBuilder.endControlFlow(")", ""); + methodBuilder.endControlFlow(")", ""); - methodBuilder.endControlFlow(")", ""); } else if (m.getReturnType().getTypeName().contains("java.util.concurrent.CompletionStage") || m.getReturnType().getTypeName().contains("java.util.concurrent.CompletableFuture")) @@ -715,19 +722,26 @@ else if (m.getReturnType().getTypeName().contains("java.util.concurrent.Completi postProcess = "."; } } + methodBuilder.addCode("exchange.dispatch( exchange.getConnection().getWorker(), () -> "); methodBuilder.beginControlFlow("", ""); methodBuilder.addCode( - "$L.thenAccept( r -> io.sinistral.proteus.server.ServerResponse.response(r)" + postProcess + "send($L) )\n\t.exceptionally( ex -> ", - "response", "exchange"); + "$L.whenComplete( (r,ex) -> ", + "response"); methodBuilder.beginControlFlow("", ""); - methodBuilder.addCode("\t\tthrow new java.util.concurrent.CompletionException(ex);\n\t"); - methodBuilder.endControlFlow(")", ""); + methodBuilder.beginControlFlow("if(ex != null)"); + methodBuilder.addCode("\texchange.putAttachment(io.undertow.server.handlers.ExceptionHandler.THROWABLE, ex);\n"); + methodBuilder.addCode("\texchange.setResponseCode(500);\n"); + methodBuilder.addCode("\texchange.endExchange();\n"); + methodBuilder.nextControlFlow("else"); + methodBuilder.addCode("\t\tio.sinistral.proteus.server.ServerResponse.response(r)" + postProcess + "send($L);","exchange"); + methodBuilder.endControlFlow(); methodBuilder.endControlFlow(")", ""); + methodBuilder.endControlFlow(")", ""); diff --git a/proteus-core/src/main/java/io/sinistral/proteus/server/handlers/ServerDefaultResponseListener.java b/proteus-core/src/main/java/io/sinistral/proteus/server/handlers/ServerDefaultResponseListener.java index c1a6371..2969cd1 100644 --- a/proteus-core/src/main/java/io/sinistral/proteus/server/handlers/ServerDefaultResponseListener.java +++ b/proteus-core/src/main/java/io/sinistral/proteus/server/handlers/ServerDefaultResponseListener.java @@ -89,7 +89,14 @@ public boolean handleDefaultResponse(HttpServerExchange exchange) errorMap.put("exceptionClass", throwable.getClass().getName()); errorMap.put("message", throwable.getMessage()); errorMap.put("path", path); - errorMap.put("code", Integer.toString(exchange.getStatusCode())); + + + if(statusCode < 300) + { + statusCode = 500; + } + + errorMap.put("code", Integer.toString(statusCode)); if(throwable.getCause() != null) { @@ -106,7 +113,7 @@ public boolean handleDefaultResponse(HttpServerExchange exchange) } - if (throwable.getStackTrace() != null && exchange.getStatusCode() >= 500 ) { + if (throwable.getStackTrace() != null && statusCode >= 500 ) { log.error("path: " + path, throwable); diff --git a/proteus-core/src/test/java/io/sinistral/proteus/test/controllers/Tests.java b/proteus-core/src/test/java/io/sinistral/proteus/test/controllers/Tests.java index d706289..70ce9f6 100644 --- a/proteus-core/src/test/java/io/sinistral/proteus/test/controllers/Tests.java +++ b/proteus-core/src/test/java/io/sinistral/proteus/test/controllers/Tests.java @@ -500,6 +500,7 @@ public CompletableFuture>> responseFutureUser( try { + Thread.sleep(2000L); future.complete(response(Map.of("status","OK")).applicationJson().ok());