Skip to content

Commit

Permalink
Merge pull request #133 from kkoz/fix-error-responses
Browse files Browse the repository at this point in the history
Set Status Code Before Writing
  • Loading branch information
sbesson authored Oct 25, 2023
2 parents cca4697 + b37fb97 commit 9effabe
Showing 1 changed file with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -451,22 +451,30 @@ private <T> Boolean handleResultFailed(
Boolean resultFailed = result.failed();
if (resultFailed) {
Throwable t = result.cause();
int statusCode = 404;
// t should always be a ReplyException, but we check anyway
if (t instanceof ReplyException) {
statusCode = ((ReplyException) t).failureCode();
response.setStatusMessage(t.getMessage());
int statusCode = ((ReplyException) t).failureCode();
// When vertx supplies a ReplyException instead of our code
// (e.g. timeout), the failure code may not be an http status code
// (it's often -1). We reset these codes to 500.
if (statusCode < 200 || statusCode > 599) {
log.error(
"Unexpected failureCode {} resetting to 500",
statusCode, t);
statusCode = 500;
}
// We must set the status code before calling write or the status code
// will be fixed to 200 (the default)
response.setStatusCode(statusCode);
response.headers().set(
"Content-Length",
String.valueOf(t.getMessage().length()));
response.write(t.getMessage());
} else {
// If it's not a ReplyException, return 500
log.error("Non-ReplyException received - this shouldn't happen");
response.setStatusCode(500);
}
if (statusCode < 200 || statusCode > 599) {
log.error(
"Unexpected failureCode {} resetting to 500",
statusCode, t);
statusCode = 500;
}
response.setStatusCode(statusCode);
}
return resultFailed;
}
Expand Down

0 comments on commit 9effabe

Please sign in to comment.