Commit 3d68072584e89246942ef1307c338d1a8e4044f5

Authored by YunaiV
1 parent 192bbf6e

fix:【framework 全局】GlobalExceptionHandler 处理 guava UncheckedExecutionException 异常…

…,https://t.zsxq.com/UszdH
yudao-framework/yudao-spring-boot-starter-web/pom.xml
@@ -53,7 +53,13 @@ @@ -53,7 +53,13 @@
53 <scope>provided</scope> <!-- 设置为 provided,主要是 GlobalExceptionHandler 使用 --> 53 <scope>provided</scope> <!-- 设置为 provided,主要是 GlobalExceptionHandler 使用 -->
54 </dependency> 54 </dependency>
55 55
56 - <!-- xss --> 56 + <!-- 工具类相关 -->
  57 + <dependency>
  58 + <groupId>com.google.guava</groupId>
  59 + <artifactId>guava</artifactId>
  60 + <scope>provided</scope> <!-- 设置为 provided,只有工具类需要使用到 -->
  61 + </dependency>
  62 +
57 <dependency> 63 <dependency>
58 <groupId>org.jsoup</groupId> 64 <groupId>org.jsoup</groupId>
59 <artifactId>jsoup</artifactId> 65 <artifactId>jsoup</artifactId>
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/core/handler/GlobalExceptionHandler.java
@@ -5,7 +5,6 @@ import cn.hutool.core.exceptions.ExceptionUtil; @@ -5,7 +5,6 @@ import cn.hutool.core.exceptions.ExceptionUtil;
5 import cn.hutool.core.map.MapUtil; 5 import cn.hutool.core.map.MapUtil;
6 import cn.hutool.core.util.ObjUtil; 6 import cn.hutool.core.util.ObjUtil;
7 import cn.hutool.core.util.StrUtil; 7 import cn.hutool.core.util.StrUtil;
8 -import cn.hutool.extra.servlet.JakartaServletUtil;  
9 import cn.iocoder.yudao.framework.common.biz.infra.logger.ApiErrorLogCommonApi; 8 import cn.iocoder.yudao.framework.common.biz.infra.logger.ApiErrorLogCommonApi;
10 import cn.iocoder.yudao.framework.common.biz.infra.logger.dto.ApiErrorLogCreateReqDTO; 9 import cn.iocoder.yudao.framework.common.biz.infra.logger.dto.ApiErrorLogCreateReqDTO;
11 import cn.iocoder.yudao.framework.common.exception.ServiceException; 10 import cn.iocoder.yudao.framework.common.exception.ServiceException;
@@ -23,6 +22,7 @@ import jakarta.validation.ConstraintViolationException; @@ -23,6 +22,7 @@ import jakarta.validation.ConstraintViolationException;
23 import jakarta.validation.ValidationException; 22 import jakarta.validation.ValidationException;
24 import lombok.AllArgsConstructor; 23 import lombok.AllArgsConstructor;
25 import lombok.extern.slf4j.Slf4j; 24 import lombok.extern.slf4j.Slf4j;
  25 +import org.apache.commons.lang3.concurrent.UncheckedExecutionException;
26 import org.springframework.http.converter.HttpMessageNotReadableException; 26 import org.springframework.http.converter.HttpMessageNotReadableException;
27 import org.springframework.security.access.AccessDeniedException; 27 import org.springframework.security.access.AccessDeniedException;
28 import org.springframework.util.Assert; 28 import org.springframework.util.Assert;
@@ -111,6 +111,9 @@ public class GlobalExceptionHandler { @@ -111,6 +111,9 @@ public class GlobalExceptionHandler {
111 if (ex instanceof AccessDeniedException) { 111 if (ex instanceof AccessDeniedException) {
112 return accessDeniedExceptionHandler(request, (AccessDeniedException) ex); 112 return accessDeniedExceptionHandler(request, (AccessDeniedException) ex);
113 } 113 }
  114 + if (ex instanceof UncheckedExecutionException && ex.getCause() != ex) {
  115 + return allExceptionHandler(request, ex.getCause());
  116 + }
114 return defaultExceptionHandler(request, ex); 117 return defaultExceptionHandler(request, ex);
115 } 118 }
116 119
@@ -267,6 +270,16 @@ public class GlobalExceptionHandler { @@ -267,6 +270,16 @@ public class GlobalExceptionHandler {
267 } 270 }
268 271
269 /** 272 /**
  273 + * 处理 Guava UncheckedExecutionException
  274 + *
  275 + * 例如说,缓存加载报错,可见 <a href="https://t.zsxq.com/UszdH">https://t.zsxq.com/UszdH</a>
  276 + */
  277 + @ExceptionHandler(value = UncheckedExecutionException.class)
  278 + public CommonResult<?> uncheckedExecutionExceptionHandler(HttpServletRequest req, UncheckedExecutionException ex) {
  279 + return allExceptionHandler(req, ex.getCause());
  280 + }
  281 +
  282 + /**
270 * 处理业务异常 ServiceException 283 * 处理业务异常 ServiceException
271 * 284 *
272 * 例如说,商品库存不足,用户手机号已存在。 285 * 例如说,商品库存不足,用户手机号已存在。
@@ -344,12 +357,12 @@ public class GlobalExceptionHandler { @@ -344,12 +357,12 @@ public class GlobalExceptionHandler {
344 errorLog.setApplicationName(applicationName); 357 errorLog.setApplicationName(applicationName);
345 errorLog.setRequestUrl(request.getRequestURI()); 358 errorLog.setRequestUrl(request.getRequestURI());
346 Map<String, Object> requestParams = MapUtil.<String, Object>builder() 359 Map<String, Object> requestParams = MapUtil.<String, Object>builder()
347 - .put("query", JakartaServletUtil.getParamMap(request))  
348 - .put("body", JakartaServletUtil.getBody(request)).build(); 360 + .put("query", ServletUtils.getParamMap(request))
  361 + .put("body", ServletUtils.getBody(request)).build();
349 errorLog.setRequestParams(JsonUtils.toJsonString(requestParams)); 362 errorLog.setRequestParams(JsonUtils.toJsonString(requestParams));
350 errorLog.setRequestMethod(request.getMethod()); 363 errorLog.setRequestMethod(request.getMethod());
351 errorLog.setUserAgent(ServletUtils.getUserAgent(request)); 364 errorLog.setUserAgent(ServletUtils.getUserAgent(request));
352 - errorLog.setUserIp(JakartaServletUtil.getClientIP(request)); 365 + errorLog.setUserIp(ServletUtils.getClientIP(request));
353 errorLog.setExceptionTime(LocalDateTime.now()); 366 errorLog.setExceptionTime(LocalDateTime.now());
354 } 367 }
355 368