From d155efe9474b287e4aed35251228c7c1cee42468 Mon Sep 17 00:00:00 2001 From: winter <2436197699@qq.com> Date: Mon, 8 Jan 2024 17:41:20 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=92=89=E9=92=89=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E4=BF=A1=E6=81=AF=E6=8E=A8=E9=80=81=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E6=A0=88=E7=9A=84=E8=AF=A6=E6=83=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/exception/GlobalExceptionHandler.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main/java/cn/teammodel/config/exception/GlobalExceptionHandler.java b/src/main/java/cn/teammodel/config/exception/GlobalExceptionHandler.java index 0962594..dc02b4b 100644 --- a/src/main/java/cn/teammodel/config/exception/GlobalExceptionHandler.java +++ b/src/main/java/cn/teammodel/config/exception/GlobalExceptionHandler.java @@ -16,6 +16,8 @@ import org.springframework.web.method.annotation.MethodArgumentTypeMismatchExcep import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.util.Objects; +import java.util.stream.Collectors; +import java.util.stream.Stream; /** @@ -77,7 +79,7 @@ public class GlobalExceptionHandler { { String requestURI = request.getRequestURI(); log.error("请求地址'{}',发生未知异常.", requestURI, e); - notificationService.send("RuntimeException 告警: " + e.getMessage()); + notificationService.send("RuntimeException 告警: " + getCausesAsString(e, 5)); return R.error(ErrorCode.SYSTEM_ERROR); } @@ -90,7 +92,7 @@ public class GlobalExceptionHandler { String requestURI = request.getRequestURI(); log.error("请求地址'{}',发生系统异常.", requestURI, e); - notificationService.send("Exception 告警: " + e.getMessage()); + notificationService.send("Exception 告警: " + getCausesAsString(e, 5)); return R.error(e.getMessage()); } @@ -115,5 +117,10 @@ public class GlobalExceptionHandler { String message = Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage(); return R.error(message); } - + private static String getCausesAsString(Throwable throwable, int maxDepth) { + return Stream.iterate(throwable, Throwable::getCause) + .limit(maxDepth) + .map(e -> e.getClass().getSimpleName() + ": " + e.getMessage()) + .collect(Collectors.joining(" <- ")); + } }