Skip to content

Commit

Permalink
Merge pull request #17 from Kerosene-Labs/fixing-error-propagation
Browse files Browse the repository at this point in the history
Fixing error propagation
  • Loading branch information
hlafaille authored Nov 26, 2024
2 parents 576ea71 + 7923566 commit 8830e80
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/java/com/kerosenelabs/kindling/HttpResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'");
}
}
Expand Down Expand Up @@ -82,7 +82,7 @@ public Builder contentType(MimeType contentType) {
return this;
}

public HttpResponse build() throws KindlingException {
public HttpResponse build() {
return new HttpResponse(this);
}
}
Expand Down
34 changes: 23 additions & 11 deletions src/java/com/kerosenelabs/kindling/KindlingServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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
Expand All @@ -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);
}
});
Expand Down
7 changes: 7 additions & 0 deletions src/java/com/kerosenelabs/kindling/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8830e80

Please sign in to comment.