From 792356688918b20fce9dcf4152816695861fc08e Mon Sep 17 00:00:00 2001 From: Hunter LaFaille Date: Tue, 26 Nov 2024 18:47:32 +0000 Subject: [PATCH] fixing errors --- .../kerosenelabs/kindling/HttpResponse.java | 6 ++-- .../kerosenelabs/kindling/KindlingServer.java | 34 +++++++++++++------ src/java/com/kerosenelabs/kindling/Main.java | 7 ++++ 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/java/com/kerosenelabs/kindling/HttpResponse.java b/src/java/com/kerosenelabs/kindling/HttpResponse.java index 3bb0512..f57c911 100644 --- a/src/java/com/kerosenelabs/kindling/HttpResponse.java +++ b/src/java/com/kerosenelabs/kindling/HttpResponse.java @@ -12,7 +12,7 @@ public class HttpResponse { private String content; private MimeType contentType; - HttpResponse(Builder builder) throws KindlingException { + HttpResponse(Builder builder) { this.httpStatus = builder.httpStatus; this.headers = builder.headers; this.content = builder.content; @@ -26,7 +26,7 @@ public class HttpResponse { // check if the content-type header has already been set for (String key : headers.keySet()) { if (key.toLowerCase().equals("content-type") && this.contentType != null) { - throw new KindlingException( + throw new RuntimeException( "Programming error, you're trying to set the 'Content-Type' header manually AND set it through 'HttpResponse.Builder'"); } } @@ -82,7 +82,7 @@ public Builder contentType(MimeType contentType) { return this; } - public HttpResponse build() throws KindlingException { + public HttpResponse build() { return new HttpResponse(this); } } diff --git a/src/java/com/kerosenelabs/kindling/KindlingServer.java b/src/java/com/kerosenelabs/kindling/KindlingServer.java index a28bd87..d4937c8 100644 --- a/src/java/com/kerosenelabs/kindling/KindlingServer.java +++ b/src/java/com/kerosenelabs/kindling/KindlingServer.java @@ -117,6 +117,12 @@ public void serve(int port, Path keystorePath, String keyStorePassword) throws K } } + private HttpResponse defaultInternalServerErrorResponse(Throwable t) { + t.printStackTrace(); + return new HttpResponse.Builder().status(HttpStatus.INTERNAL_SERVER_ERROR).content("Internal Server Error") + .build(); + } + /** * Dispatch a worker to handle a particular request * @@ -130,20 +136,26 @@ private void dispatchWorker(SSLSocket sslSocket) { InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); OutputStream outputStream = sslSocket.getOutputStream();) { + HttpResponse response = null; // parse our http request - HttpRequest httpRequest = new HttpRequest(bufferedReader); - - // iterate over request handlers, finding one that can take this request - HttpResponse response = null; - for (RequestHandler requestHandler : requestHandlers) { - if (requestHandler.accepts(httpRequest)) { - try { - response = requestHandler.handle(httpRequest); - } catch (Exception e) { - response = requestHandler.handleError(e); + try { + HttpRequest httpRequest = new HttpRequest(bufferedReader); + + // iterate over request handlers, finding one that can take this request. + // note: we catch all exceptions here during the handle() so that way we can + // pass the error handling to the request handler. + for (RequestHandler requestHandler : requestHandlers) { + if (requestHandler.accepts(httpRequest)) { + try { + response = requestHandler.handle(httpRequest); + } catch (Exception e) { + response = requestHandler.handleError(e); + } } } + } catch (KindlingException e) { + response = defaultInternalServerErrorResponse(e); } // if there were no request handlers @@ -154,7 +166,7 @@ private void dispatchWorker(SSLSocket sslSocket) { // write our response outputStream.write(response.toString().getBytes()); - } catch (IOException | KindlingException e) { + } catch (IOException e) { throw new RuntimeException(e); } }); diff --git a/src/java/com/kerosenelabs/kindling/Main.java b/src/java/com/kerosenelabs/kindling/Main.java index d3a75dd..2b26ea1 100644 --- a/src/java/com/kerosenelabs/kindling/Main.java +++ b/src/java/com/kerosenelabs/kindling/Main.java @@ -34,6 +34,13 @@ public HttpResponse handle(HttpRequest httpRequest) throws KindlingException { .content("{\"key\": \"value\"}") .build(); } + + @Override + public HttpResponse handleError(Throwable t) throws KindlingException { + return new HttpResponse.Builder().status(HttpStatus.INTERNAL_SERVER_ERROR) + .content(t.getStackTrace().toString()) + .build(); + } }); // serve our server