From 6544b92de24bd9a66b3a53ff3e9e90abe887ee66 Mon Sep 17 00:00:00 2001 From: Pil0tXia Date: Sun, 14 Apr 2024 16:12:10 +0800 Subject: [PATCH] Catch more exceptions --- .../admin/handler/AbstractHttpHandler.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/AbstractHttpHandler.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/AbstractHttpHandler.java index 43f9753dfd..cdfe4e163a 100644 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/AbstractHttpHandler.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/AbstractHttpHandler.java @@ -85,6 +85,12 @@ protected void writeUnauthorized(ChannelHandlerContext ctx, String message) { writeJson(ctx, json, HttpResponseStatus.UNAUTHORIZED); } + protected void writeInternalServerError(ChannelHandlerContext ctx, String message) { + Result 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. */ @@ -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()); + } } }