Skip to content

Commit

Permalink
Catch more exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Pil0tXia committed Apr 14, 2024
1 parent 9e9c96c commit 6544b92
Showing 1 changed file with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ protected void writeUnauthorized(ChannelHandlerContext ctx, String message) {
writeJson(ctx, json, HttpResponseStatus.UNAUTHORIZED);
}

protected void writeInternalServerError(ChannelHandlerContext ctx, String message) {
Result<String> result = new Result<>(message);
String json = JSON.toJSONString(result, JSONWriter.Feature.WriteNulls);
writeJson(ctx, json, HttpResponseStatus.INTERNAL_SERVER_ERROR);
}

/**
* Use {@link HttpResponseUtils#buildHttpResponse} to build {@link HttpResponse} param.
*/
Expand Down Expand Up @@ -113,12 +119,17 @@ public void handle(HttpRequest httpRequest, ChannelHandlerContext ctx) throws Ex
break;
default: // do nothing
}
} catch (IllegalArgumentException e) {
} catch (RuntimeException e) {
StackTraceElement element = e.getStackTrace()[0];
String className = element.getClassName();
String handlerName = className.substring(className.lastIndexOf(".") + 1);
log.warn("Admin handler {}:{} - {}", handlerName, element.getLineNumber(), e.getMessage());
writeBadRequest(ctx, e.getMessage());
if (e instanceof IllegalArgumentException) {
log.warn("Admin endpoint {}:{} - {}", handlerName, element.getLineNumber(), e.getMessage());
writeBadRequest(ctx, e.getMessage());
} else {
log.error("Admin endpoint {}:{} - {}", handlerName, element.getLineNumber(), e.getMessage(), e);
writeInternalServerError(ctx, e.getMessage());
}
}
}

Expand Down

0 comments on commit 6544b92

Please sign in to comment.