From cf4fb49ec3d8ce1fb31c82522d4e43cbf647c8c3 Mon Sep 17 00:00:00 2001 From: liaozan <378024053@qq.com> Date: Thu, 27 Apr 2023 16:15:38 +0800 Subject: [PATCH] Update DefaultGlobalExceptionHandler --- .../DefaultGlobalExceptionHandler.java | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/commons/web-common/src/main/java/com/schbrain/common/web/exception/DefaultGlobalExceptionHandler.java b/commons/web-common/src/main/java/com/schbrain/common/web/exception/DefaultGlobalExceptionHandler.java index ff0a986..529db43 100644 --- a/commons/web-common/src/main/java/com/schbrain/common/web/exception/DefaultGlobalExceptionHandler.java +++ b/commons/web-common/src/main/java/com/schbrain/common/web/exception/DefaultGlobalExceptionHandler.java @@ -2,6 +2,7 @@ package com.schbrain.common.web.exception; import cn.hutool.core.exceptions.ExceptionUtil; import cn.hutool.core.util.StrUtil; +import com.schbrain.common.constants.ResponseActionConstants; import com.schbrain.common.exception.BaseException; import com.schbrain.common.util.EnvUtils; import com.schbrain.common.web.result.ResponseDTO; @@ -40,7 +41,8 @@ public class DefaultGlobalExceptionHandler implements GlobalExceptionHandler { /************************************* Base Exception Handing *************************************/ @ExceptionHandler(BaseException.class) public ResponseDTO handleBaseException(BaseException ex) { - return loggingThenBuildResponse(ex, ex.getCode()); + log.error(ex.getMessage()); + return buildResponse(ex, ex.getCode(), ex.getAction(), ex.getMessage()); } /************************************* Common Exception Handing *************************************/ @@ -197,22 +199,23 @@ public class DefaultGlobalExceptionHandler implements GlobalExceptionHandler { return buildResponse(rootCause, errorCode, rootCause.getMessage()); } - protected ResponseDTO buildResponse(Throwable throwable, int errorCode, String message) { - boolean production = EnvUtils.isProduction(); - ResponseDTO responseDTO = getExceptionResponseMapping(throwable, production); + protected ResponseDTO buildResponse(Throwable throwable, int code, String message) { + return buildResponse(throwable, code, ResponseActionConstants.ALERT, message); + } + + protected ResponseDTO buildResponse(Throwable throwable, int code, int action, String message) { + boolean isProduction = EnvUtils.isProduction(); + ResponseDTO responseDTO = getExceptionResponseMapping(throwable, isProduction); if (responseDTO != null) { return responseDTO; } - if (production) { - return ResponseDTO.error("系统错误", errorCode); - } - if (StringUtils.isBlank(message)) { - return ResponseDTO.error("系统错误", errorCode); + if (isProduction || StringUtils.isBlank(message)) { + return ResponseDTO.error("系统错误", code, action); } - return ResponseDTO.error(message, errorCode); + return ResponseDTO.error(message, code, action); } - protected ResponseDTO getExceptionResponseMapping(Throwable throwable, boolean production) { + protected ResponseDTO getExceptionResponseMapping(Throwable throwable, boolean isProduction) { return null; } -- GitLab