// the middleware was not mounted on this uri, skip to the next entry
handle(null);
}
}
} else {
HttpServerResponse response = request.response();
// reached the end and no handler was able to answer the request
response.setStatusCode(404);
response.setStatusMessage(HttpResponseStatus.valueOf(404).reasonPhrase());
if (errorHandler != null) {
errorHandler.handle(request, null);
} else {
response.end(HttpResponseStatus.valueOf(404).reasonPhrase());
}
}
} else {
request.put("error", error);
if (errorHandler != null) {
errorHandler.handle(request, null);
} else {
HttpServerResponse response = request.response();
int errorCode;
// if the error was set on the response use it
if (response.getStatusCode() >= 400) {
errorCode = response.getStatusCode();
} else {
// if it was set as the error object use it
if (error instanceof Number) {
errorCode = ((Number) error).intValue();
} else if (error instanceof YokeException) {
errorCode = ((YokeException) error).getErrorCode().intValue();
} else if (error instanceof JsonObject) {
errorCode = ((JsonObject) error).getInteger("errorCode", 500);
} else if (error instanceof Map) {
Integer tmp = (Integer) ((Map) error).get("errorCode");
errorCode = tmp != null ? tmp : 500;
} else {
// default error code
errorCode = 500;
}
}
response.setStatusCode(errorCode);
response.setStatusMessage(HttpResponseStatus.valueOf(errorCode).reasonPhrase());
response.end(HttpResponseStatus.valueOf(errorCode).reasonPhrase());
}
}
}
}.handle(null);
}