From 29e7c5dc41a841c9e324b0e076d7980e28ebd266 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Wed, 9 Aug 2023 10:06:36 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=20=E5=85=A8?= =?UTF-8?q?=E5=B1=80=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86=E5=99=A8=20?= =?UTF-8?q?=E4=B8=9A=E5=8A=A1=E5=BC=82=E5=B8=B8=E4=B8=8D=E8=BE=93=E5=87=BA?= =?UTF-8?q?=E5=85=B7=E4=BD=93=E5=A0=86=E6=A0=88=E4=BF=A1=E6=81=AF=20?= =?UTF-8?q?=E5=87=8F=E5=B0=91=E6=97=A0=E7=94=A8=E6=97=A5=E5=BF=97=E5=AD=98?= =?UTF-8?q?=E5=82=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../handler/GlobalExceptionHandler.java | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/ruoyi-common/ruoyi-common-security/src/main/java/org/dromara/common/security/handler/GlobalExceptionHandler.java b/ruoyi-common/ruoyi-common-security/src/main/java/org/dromara/common/security/handler/GlobalExceptionHandler.java index d9ee05f4..1809c613 100644 --- a/ruoyi-common/ruoyi-common-security/src/main/java/org/dromara/common/security/handler/GlobalExceptionHandler.java +++ b/ruoyi-common/ruoyi-common-security/src/main/java/org/dromara/common/security/handler/GlobalExceptionHandler.java @@ -12,6 +12,7 @@ import lombok.extern.slf4j.Slf4j; import org.dromara.common.core.domain.R; import org.dromara.common.core.exception.DemoModeException; import org.dromara.common.core.exception.ServiceException; +import org.dromara.common.core.exception.base.BaseException; import org.dromara.common.core.utils.StreamUtils; import org.springframework.context.support.DefaultMessageSourceResolvable; import org.springframework.validation.BindException; @@ -77,18 +78,27 @@ public class GlobalExceptionHandler { */ @ExceptionHandler(ServiceException.class) public R handleServiceException(ServiceException e, HttpServletRequest request) { - log.error(e.getMessage(), e); + log.error(e.getMessage()); Integer code = e.getCode(); return ObjectUtil.isNotNull(code) ? R.fail(code, e.getMessage()) : R.fail(e.getMessage()); } + /** + * 业务异常 + */ + @ExceptionHandler(BaseException.class) + public R handleBaseException(BaseException e, HttpServletRequest request) { + log.error(e.getMessage()); + return R.fail(e.getMessage()); + } + /** * 请求路径中缺少必需的路径变量 */ @ExceptionHandler(MissingPathVariableException.class) public R handleMissingPathVariableException(MissingPathVariableException e, HttpServletRequest request) { String requestURI = request.getRequestURI(); - log.error("请求路径中缺少必需的路径变量'{}',发生系统异常.", requestURI, e); + log.error("请求路径中缺少必需的路径变量'{}',发生系统异常.", requestURI); return R.fail(String.format("请求路径中缺少必需的路径变量[%s]", e.getVariableName())); } @@ -98,7 +108,7 @@ public class GlobalExceptionHandler { @ExceptionHandler(MethodArgumentTypeMismatchException.class) public R handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e, HttpServletRequest request) { String requestURI = request.getRequestURI(); - log.error("请求参数类型不匹配'{}',发生系统异常.", requestURI, e); + log.error("请求参数类型不匹配'{}',发生系统异常.", requestURI); return R.fail(String.format("请求参数类型不匹配,参数[%s]要求类型为:'%s',但输入值为:'%s'", e.getName(), e.getRequiredType().getName(), e.getValue())); } @@ -127,7 +137,7 @@ public class GlobalExceptionHandler { */ @ExceptionHandler(BindException.class) public R handleBindException(BindException e) { - log.error(e.getMessage(), e); + log.error(e.getMessage()); String message = StreamUtils.join(e.getAllErrors(), DefaultMessageSourceResolvable::getDefaultMessage, ", "); return R.fail(message); } @@ -137,7 +147,7 @@ public class GlobalExceptionHandler { */ @ExceptionHandler(ConstraintViolationException.class) public R constraintViolationException(ConstraintViolationException e) { - log.error(e.getMessage(), e); + log.error(e.getMessage()); String message = StreamUtils.join(e.getConstraintViolations(), ConstraintViolation::getMessage, ", "); return R.fail(message); } @@ -147,7 +157,7 @@ public class GlobalExceptionHandler { */ @ExceptionHandler(MethodArgumentNotValidException.class) public R handleMethodArgumentNotValidException(MethodArgumentNotValidException e) { - log.error(e.getMessage(), e); + log.error(e.getMessage()); String message = e.getBindingResult().getFieldError().getDefaultMessage(); return R.fail(message); }