Skip to content

Commit

Permalink
Improve error handlers.
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed Jan 6, 2020
1 parent 20c6753 commit e1a7de4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 19 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.**


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/**
*
*/

package io.sinistral.proteus.server;

import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/**
*
*/

package io.sinistral.proteus.server.handlers;

import com.google.inject.Inject;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, String> errorMap = new HashMap<>();

final String path = exchange.getRelativePath();
Throwable throwable = exchange.getAttachment(DefaultResponseListener.EXCEPTION);

if (throwable == null) {
final String reason = StatusCodes.getReason(statusCode);
Expand Down

0 comments on commit e1a7de4

Please sign in to comment.