Commit ee3e507f07f4284dfbcf09dd222fca9c03228a64

Authored by YunaiV
1 parent 88ac5be8

【优化】GlobalExceptionHandler 新增 IGNORE_ERROR_MESSAGES,用于忽略“无效的刷新令牌”等非关键提示

yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/core/handler/GlobalExceptionHandler.java
@@ -6,6 +6,7 @@ import cn.hutool.core.util.StrUtil; @@ -6,6 +6,7 @@ import cn.hutool.core.util.StrUtil;
6 import cn.iocoder.yudao.framework.apilog.core.service.ApiErrorLogFrameworkService; 6 import cn.iocoder.yudao.framework.apilog.core.service.ApiErrorLogFrameworkService;
7 import cn.iocoder.yudao.framework.common.exception.ServiceException; 7 import cn.iocoder.yudao.framework.common.exception.ServiceException;
8 import cn.iocoder.yudao.framework.common.pojo.CommonResult; 8 import cn.iocoder.yudao.framework.common.pojo.CommonResult;
  9 +import cn.iocoder.yudao.framework.common.util.collection.SetUtils;
9 import cn.iocoder.yudao.framework.common.util.json.JsonUtils; 10 import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
10 import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils; 11 import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils;
11 import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; 12 import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils;
@@ -32,6 +33,7 @@ import org.springframework.web.servlet.NoHandlerFoundException; @@ -32,6 +33,7 @@ import org.springframework.web.servlet.NoHandlerFoundException;
32 33
33 import java.time.LocalDateTime; 34 import java.time.LocalDateTime;
34 import java.util.Map; 35 import java.util.Map;
  36 +import java.util.Set;
35 37
36 import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.*; 38 import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.*;
37 39
@@ -45,6 +47,11 @@ import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeC @@ -45,6 +47,11 @@ import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeC
45 @Slf4j 47 @Slf4j
46 public class GlobalExceptionHandler { 48 public class GlobalExceptionHandler {
47 49
  50 + /**
  51 + * 忽略的 ServiceException 错误提示,避免打印过多 logger
  52 + */
  53 + public static final Set<String> IGNORE_ERROR_MESSAGES = SetUtils.asSet("无效的刷新令牌");
  54 +
48 @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") 55 @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
49 private final String applicationName; 56 private final String applicationName;
50 57
@@ -199,7 +206,10 @@ public class GlobalExceptionHandler { @@ -199,7 +206,10 @@ public class GlobalExceptionHandler {
199 */ 206 */
200 @ExceptionHandler(value = ServiceException.class) 207 @ExceptionHandler(value = ServiceException.class)
201 public CommonResult<?> serviceExceptionHandler(ServiceException ex) { 208 public CommonResult<?> serviceExceptionHandler(ServiceException ex) {
202 - log.info("[serviceExceptionHandler]", ex); 209 + if (!IGNORE_ERROR_MESSAGES.contains(ex.getMessage())) {
  210 + // 不包含的时候,才进行打印,避免 ex 堆栈过多
  211 + log.info("[serviceExceptionHandler]", ex);
  212 + }
203 return CommonResult.error(ex.getCode(), ex.getMessage()); 213 return CommonResult.error(ex.getCode(), ex.getMessage());
204 } 214 }
205 215