From e1a7de453d79acf57415e7e7cd0ae834ea784b26 Mon Sep 17 00:00:00 2001 From: Joshua Bauer Date: Mon, 6 Jan 2020 09:12:33 -0800 Subject: [PATCH] Improve error handlers. --- CHANGELOG.md | 10 ++++++++++ .../sinistral/proteus/server/ServerRequest.java | 3 ++- .../sinistral/proteus/server/ServerResponse.java | 12 +++++++----- .../handlers/ServerDefaultHttpHandler.java | 16 +++++----------- .../handlers/ServerDefaultResponseListener.java | 12 ++++++++++-- 5 files changed, 34 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 506538a..644c8a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,16 @@ Proteus Changelog. ## Unreleased ### No issue +**Set default content type for exceptions.** + + +[20c67536c5ec75c](https://github.com/noboomu/proteus/commit/20c67536c5ec75c) joshua bauer *2020-01-05 05:36:36* + +**Revert to previous openhft compiler version.** + + +[ba63bd0fc852aaa](https://github.com/noboomu/proteus/commit/ba63bd0fc852aaa) joshua bauer *2020-12-30 19:35:52* + **Further css fixes.** diff --git a/proteus-core/src/main/java/io/sinistral/proteus/server/ServerRequest.java b/proteus-core/src/main/java/io/sinistral/proteus/server/ServerRequest.java index 9f8a0e2..fa2d021 100644 --- a/proteus-core/src/main/java/io/sinistral/proteus/server/ServerRequest.java +++ b/proteus-core/src/main/java/io/sinistral/proteus/server/ServerRequest.java @@ -8,6 +8,7 @@ import io.undertow.security.api.SecurityContext; import io.undertow.server.DefaultResponseListener; import io.undertow.server.HttpServerExchange; +import io.undertow.server.handlers.ExceptionHandler; import io.undertow.server.handlers.form.FormData; import io.undertow.server.handlers.form.FormDataParser; import io.undertow.server.handlers.form.FormEncodedDataDefinition; @@ -32,7 +33,7 @@ public class ServerRequest { protected static final Receiver.ErrorCallback ERROR_CALLBACK = (exchange, e) -> { - exchange.putAttachment(DefaultResponseListener.EXCEPTION, e); + exchange.putAttachment(ExceptionHandler.THROWABLE, e); exchange.endExchange(); }; diff --git a/proteus-core/src/main/java/io/sinistral/proteus/server/ServerResponse.java b/proteus-core/src/main/java/io/sinistral/proteus/server/ServerResponse.java index 9255794..7fe041b 100644 --- a/proteus-core/src/main/java/io/sinistral/proteus/server/ServerResponse.java +++ b/proteus-core/src/main/java/io/sinistral/proteus/server/ServerResponse.java @@ -1,6 +1,4 @@ -/** - * - */ + package io.sinistral.proteus.server; import com.fasterxml.jackson.databind.ObjectMapper; @@ -13,6 +11,7 @@ import io.undertow.server.HttpHandler; import io.undertow.server.HttpServerExchange; import io.undertow.server.handlers.Cookie; +import io.undertow.server.handlers.ExceptionHandler; import io.undertow.util.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -561,8 +560,11 @@ public void send(final HttpHandler handler, final HttpServerExchange exchange) t if (hasError) { - exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, javax.ws.rs.core.MediaType.APPLICATION_JSON); - exchange.putAttachment(DefaultResponseListener.EXCEPTION, throwable); + if(this.status == StatusCodes.OK) + { + exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR); + } + exchange.putAttachment(ExceptionHandler.THROWABLE, throwable); exchange.endExchange(); return; } diff --git a/proteus-core/src/main/java/io/sinistral/proteus/server/handlers/ServerDefaultHttpHandler.java b/proteus-core/src/main/java/io/sinistral/proteus/server/handlers/ServerDefaultHttpHandler.java index 105adcb..4ca60d1 100644 --- a/proteus-core/src/main/java/io/sinistral/proteus/server/handlers/ServerDefaultHttpHandler.java +++ b/proteus-core/src/main/java/io/sinistral/proteus/server/handlers/ServerDefaultHttpHandler.java @@ -1,6 +1,4 @@ -/** - * - */ + package io.sinistral.proteus.server.handlers; import com.google.inject.Inject; @@ -10,6 +8,7 @@ import io.undertow.server.HttpHandler; import io.undertow.server.HttpServerExchange; import io.undertow.server.RoutingHandler; +import io.undertow.server.handlers.ExceptionHandler; import io.undertow.util.HeaderMap; import io.undertow.util.HeaderValues; import io.undertow.util.Headers; @@ -68,17 +67,12 @@ public void handleRequest(final HttpServerExchange exchange) throws Exception } try { + next.handleRequest(exchange); - } catch (Exception e) { - exchange.putAttachment(DefaultResponseListener.EXCEPTION,e); + } catch (Exception e) { - if(e instanceof ServerException) - { - ServerException serverException = (ServerException) e; - exchange.setStatusCode(serverException.getStatus()); - exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, javax.ws.rs.core.MediaType.APPLICATION_JSON); - } + exchange.putAttachment(ExceptionHandler.THROWABLE,e); defaultResponseListener.handleDefaultResponse(exchange); } 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 f7ed099..f0643da 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 @@ -12,6 +12,7 @@ import io.sinistral.proteus.server.predicates.ServerPredicates; import io.undertow.server.DefaultResponseListener; import io.undertow.server.HttpServerExchange; +import io.undertow.server.handlers.ExceptionHandler; import io.undertow.util.Headers; import io.undertow.util.StatusCodes; import org.slf4j.Logger; @@ -50,11 +51,18 @@ public boolean handleDefaultResponse(HttpServerExchange exchange) int statusCode = exchange.getStatusCode(); - if (statusCode >= 400) { + Throwable throwable = exchange.getAttachment(ExceptionHandler.THROWABLE); + + if(throwable == null) + { + throwable = exchange.getAttachment(DefaultResponseListener.EXCEPTION); + } + + if (statusCode >= 400 || throwable != null) { final Map errorMap = new HashMap<>(); + final String path = exchange.getRelativePath(); - Throwable throwable = exchange.getAttachment(DefaultResponseListener.EXCEPTION); if (throwable == null) { final String reason = StatusCodes.getReason(statusCode);