From e04c9584e38d52c4e03b37a7adb61190d32b72a2 Mon Sep 17 00:00:00 2001 From: YunaiV <> Date: Sun, 19 Jul 2020 03:55:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20Dubbo=20Exception=20Filter?= =?UTF-8?q?=20=E7=9A=84=E5=A4=84=E7=90=86=EF=BC=8C=E9=92=88=E5=AF=B9?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E5=BC=82=E5=B8=B8=EF=BC=8C=E8=BF=98=E6=98=AF?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E6=8A=9B=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../filter/DubboProviderExceptionFilter.java | 29 ++++++++----------- .../core/handler/GlobalExceptionHandler.java | 12 ++++++-- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/common/mall-spring-boot-starter-dubbo/src/main/java/cn/iocoder/mall/dubbo/core/filter/DubboProviderExceptionFilter.java b/common/mall-spring-boot-starter-dubbo/src/main/java/cn/iocoder/mall/dubbo/core/filter/DubboProviderExceptionFilter.java index 43cab7eb7..7df2991fd 100644 --- a/common/mall-spring-boot-starter-dubbo/src/main/java/cn/iocoder/mall/dubbo/core/filter/DubboProviderExceptionFilter.java +++ b/common/mall-spring-boot-starter-dubbo/src/main/java/cn/iocoder/mall/dubbo/core/filter/DubboProviderExceptionFilter.java @@ -15,7 +15,8 @@ import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolationException; import java.lang.reflect.Type; -import static cn.iocoder.common.framework.exception.enums.GlobalErrorCodeConstants.*; +import static cn.iocoder.common.framework.exception.enums.GlobalErrorCodeConstants.BAD_REQUEST; +import static cn.iocoder.common.framework.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR; @Activate(group = CommonConstants.PROVIDER) public class DubboProviderExceptionFilter implements Filter, Filter.Listener { @@ -31,30 +32,24 @@ public class DubboProviderExceptionFilter implements Filter, Filter.Listener { public void onResponse(Result appResponse, Invoker invoker, Invocation invocation) { if (appResponse.hasException() && GenericService.class != invoker.getInterface()) { try { - // 转换异常 + // 1. 转换异常 Throwable exception = appResponse.getException(); - // 1. 参数校验异常 + // 1.1 参数校验异常 if (exception instanceof ConstraintViolationException) { exception = this.constraintViolationExceptionHandler((ConstraintViolationException) exception); - // 2. ServiceException 业务异常,因为不会有序列化问题,所以无需处理 + // 1. ServiceException 业务异常,因为不会有序列化问题,所以无需处理 } else if (exception instanceof ServiceException) { - // 3. 其它异常,转换成 ServiceException 业务异常,避免可能存在的反序列化问题 + // 1.3 其它异常,转换成 ServiceException 业务异常,避免可能存在的反序列化问题 } else { exception = this.defaultExceptionHandler(exception, invocation); assert exception != null; } - // 根据不同的方法 schema 返回结果 - // 第一种情况,返回参数类型是 CommonResult 的情况,则将 ServiceException 转换成 CommonResult - if (isReturnCommonResult(invocation)) { - // 清空异常 - appResponse.setException(null); - // 设置结果 - if (exception instanceof ServiceException) { - appResponse.setValue(CommonResult.error((ServiceException) exception)); - } else { - appResponse.setValue(CommonResult.error((GlobalException) exception)); - } - // 第二种情况,未包装成 CommonResult 的情况,则直接抛出 ServiceException 异常 + // 2. 根据不同的方法 schema 返回结果 + // 2.1 如果是 ServiceException 异常,并且返回参数类型是 CommonResult 的情况,则将转换成 CommonResult 返回 + if (isReturnCommonResult(invocation) && exception instanceof ServiceException) { + appResponse.setException(null); // 一定要清空异常 + appResponse.setValue(CommonResult.error((ServiceException) exception)); + // 2.2 如果是 GlobalException 全局异常,则直接抛出 } else { appResponse.setException(exception); } diff --git a/common/mall-spring-boot-starter-web/src/main/java/cn/iocoder/mall/web/core/handler/GlobalExceptionHandler.java b/common/mall-spring-boot-starter-web/src/main/java/cn/iocoder/mall/web/core/handler/GlobalExceptionHandler.java index b4c672f1b..7a5dff9c3 100644 --- a/common/mall-spring-boot-starter-web/src/main/java/cn/iocoder/mall/web/core/handler/GlobalExceptionHandler.java +++ b/common/mall-spring-boot-starter-web/src/main/java/cn/iocoder/mall/web/core/handler/GlobalExceptionHandler.java @@ -160,9 +160,15 @@ public class GlobalExceptionHandler { */ @ExceptionHandler(value = GlobalException.class) public CommonResult globalExceptionHandler(HttpServletRequest req, GlobalException ex) { - logger.error("[globalExceptionHandler]", ex); - // 插入异常日志 - this.createExceptionLog(req, ex); + // 系统异常时,才打印异常日志 + if (INTERNAL_SERVER_ERROR.getCode().equals(ex.getCode())) { + logger.error("[globalExceptionHandler]", ex); + // 插入异常日志 + this.createExceptionLog(req, ex); + // 普通全局异常,打印 info 日志即可 + } else { + logger.info("[globalExceptionHandler]", ex); + } // 返回 ERROR CommonResult return CommonResult.error(ex); }