From 20d00c86496fcb75b4e313ca9f41b0f322250471 Mon Sep 17 00:00:00 2001 From: joshua bauer Date: Wed, 25 Jul 2018 18:00:54 -0700 Subject: [PATCH] Added ServerException. --- .../proteus/server/ServerResponse.java | 26 +++-- .../server/exceptions/ServerException.java | 104 ++++++++++++++++++ 2 files changed, 120 insertions(+), 10 deletions(-) create mode 100644 src/main/java/io/sinistral/proteus/server/exceptions/ServerException.java diff --git a/src/main/java/io/sinistral/proteus/server/ServerResponse.java b/src/main/java/io/sinistral/proteus/server/ServerResponse.java index 3827b02..ed62c55 100644 --- a/src/main/java/io/sinistral/proteus/server/ServerResponse.java +++ b/src/main/java/io/sinistral/proteus/server/ServerResponse.java @@ -538,8 +538,23 @@ public void send(final HttpServerExchange exchange) throws RuntimeException public void send(final HttpHandler handler, final HttpServerExchange exchange) throws RuntimeException { + + + final boolean hasBody = this.body != null; + final boolean hasEntity = this.entity != null; + final boolean hasError = this.throwable != null; + exchange.setStatusCode(this.status); + + + if (hasError) + { + exchange.putAttachment(DefaultResponseListener.EXCEPTION, throwable); + exchange.endExchange(); + return; + } + if(this.location != null) { exchange.getResponseHeaders().put(Headers.LOCATION, this.location); @@ -551,10 +566,6 @@ public void send(final HttpHandler handler, final HttpServerExchange exchange) t return; } - final boolean hasBody = this.body != null; - final boolean hasEntity = this.entity != null; - final boolean hasError = this.throwable != null; - if (this.hasHeaders) { long itr = this.headers.fastIterateNonEmpty(); @@ -593,12 +604,7 @@ else if (ServerPredicates.ACCEPT_XML_PREDICATE.resolve(exchange)) } } - if (hasError) - { - exchange.putAttachment(DefaultResponseListener.EXCEPTION, throwable); - - return; - } + if (hasBody) { diff --git a/src/main/java/io/sinistral/proteus/server/exceptions/ServerException.java b/src/main/java/io/sinistral/proteus/server/exceptions/ServerException.java new file mode 100644 index 0000000..52ac978 --- /dev/null +++ b/src/main/java/io/sinistral/proteus/server/exceptions/ServerException.java @@ -0,0 +1,104 @@ +/** + * + */ +package io.sinistral.proteus.server.exceptions; + +import javax.ws.rs.core.Response.Status; + +/** + * @author jbauer + * + */ +public class ServerException extends Exception +{ + /** + * + */ + private static final long serialVersionUID = 8360356916374374408L; + + private Integer status = 500; + + + + public ServerException(int status) + { + super(); + this.status = status; + } + + + public ServerException(String message, Throwable cause, int status) + { + super(message, cause); + this.status = status; + } + + /** + * @param message + */ + public ServerException(String message, int status) + { + super(message); + this.status = status; + } + + + /** + * @param cause + */ + public ServerException(Throwable cause, int status) + { + super(cause); + this.status = status; + } + + public ServerException(Status status) + { + super(); + this.status = status.getStatusCode(); + } + + + public ServerException(String message, Throwable cause, Status status) + { + super(message, cause); + this.status = status.getStatusCode(); + } + + /** + * @param message + */ + public ServerException(String message, Status status) + { + super(message); + this.status = status.getStatusCode(); + } + + + /** + * @param cause + */ + public ServerException(Throwable cause, Status status) + { + super(cause); + this.status = status.getStatusCode(); + } + + /** + * @return the status + */ + public Integer getStatus() + { + return status; + } + + /** + * @param status the status to set + */ + public void setStatus(Integer status) + { + this.status = status; + } + + +}