Commit 3d68072584e89246942ef1307c338d1a8e4044f5
1 parent
192bbf6e
fix:【framework 全局】GlobalExceptionHandler 处理 guava UncheckedExecutionException 异常…
…,https://t.zsxq.com/UszdH
Showing
2 changed files
with
24 additions
and
5 deletions
yudao-framework/yudao-spring-boot-starter-web/pom.xml
| ... | ... | @@ -53,7 +53,13 @@ |
| 53 | 53 | <scope>provided</scope> <!-- 设置为 provided,主要是 GlobalExceptionHandler 使用 --> |
| 54 | 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 | 63 | <dependency> |
| 58 | 64 | <groupId>org.jsoup</groupId> |
| 59 | 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 | 5 | import cn.hutool.core.map.MapUtil; |
| 6 | 6 | import cn.hutool.core.util.ObjUtil; |
| 7 | 7 | import cn.hutool.core.util.StrUtil; |
| 8 | -import cn.hutool.extra.servlet.JakartaServletUtil; | |
| 9 | 8 | import cn.iocoder.yudao.framework.common.biz.infra.logger.ApiErrorLogCommonApi; |
| 10 | 9 | import cn.iocoder.yudao.framework.common.biz.infra.logger.dto.ApiErrorLogCreateReqDTO; |
| 11 | 10 | import cn.iocoder.yudao.framework.common.exception.ServiceException; |
| ... | ... | @@ -23,6 +22,7 @@ import jakarta.validation.ConstraintViolationException; |
| 23 | 22 | import jakarta.validation.ValidationException; |
| 24 | 23 | import lombok.AllArgsConstructor; |
| 25 | 24 | import lombok.extern.slf4j.Slf4j; |
| 25 | +import org.apache.commons.lang3.concurrent.UncheckedExecutionException; | |
| 26 | 26 | import org.springframework.http.converter.HttpMessageNotReadableException; |
| 27 | 27 | import org.springframework.security.access.AccessDeniedException; |
| 28 | 28 | import org.springframework.util.Assert; |
| ... | ... | @@ -111,6 +111,9 @@ public class GlobalExceptionHandler { |
| 111 | 111 | if (ex instanceof AccessDeniedException) { |
| 112 | 112 | return accessDeniedExceptionHandler(request, (AccessDeniedException) ex); |
| 113 | 113 | } |
| 114 | + if (ex instanceof UncheckedExecutionException && ex.getCause() != ex) { | |
| 115 | + return allExceptionHandler(request, ex.getCause()); | |
| 116 | + } | |
| 114 | 117 | return defaultExceptionHandler(request, ex); |
| 115 | 118 | } |
| 116 | 119 | |
| ... | ... | @@ -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 | 283 | * 处理业务异常 ServiceException |
| 271 | 284 | * |
| 272 | 285 | * 例如说,商品库存不足,用户手机号已存在。 |
| ... | ... | @@ -344,12 +357,12 @@ public class GlobalExceptionHandler { |
| 344 | 357 | errorLog.setApplicationName(applicationName); |
| 345 | 358 | errorLog.setRequestUrl(request.getRequestURI()); |
| 346 | 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 | 362 | errorLog.setRequestParams(JsonUtils.toJsonString(requestParams)); |
| 350 | 363 | errorLog.setRequestMethod(request.getMethod()); |
| 351 | 364 | errorLog.setUserAgent(ServletUtils.getUserAgent(request)); |
| 352 | - errorLog.setUserIp(JakartaServletUtil.getClientIP(request)); | |
| 365 | + errorLog.setUserIp(ServletUtils.getClientIP(request)); | |
| 353 | 366 | errorLog.setExceptionTime(LocalDateTime.now()); |
| 354 | 367 | } |
| 355 | 368 | ... | ... |