| | |
| | | if (ex instanceof AccessDeniedException) { |
| | | return accessDeniedExceptionHandler(request, (AccessDeniedException) ex); |
| | | } |
| | | // 参数校验类异常(如 Assert.notNull 抛出的 IllegalArgumentException)属于客户端问题,不应按 500 处理 |
| | | if (ex instanceof IllegalArgumentException) { |
| | | return illegalArgumentExceptionHandler((IllegalArgumentException) ex); |
| | | } |
| | | return defaultExceptionHandler(request, ex); |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | /** |
| | | * 处理参数非法异常 |
| | | * |
| | | * 来源是,业务代码中使用 {@link Assert#notNull} 等断言校验入参,或缓存注解使用空 key。 |
| | | * 这类问题属于调用方传参不正确,返回 400 而不是 500,避免前端把参数问题误判为系统故障。 |
| | | */ |
| | | @ExceptionHandler(value = IllegalArgumentException.class) |
| | | public CommonResult<?> illegalArgumentExceptionHandler(IllegalArgumentException ex) { |
| | | log.warn("[illegalArgumentExceptionHandler]", ex); |
| | | return CommonResult.error(BAD_REQUEST.getCode(), ex.getMessage()); |
| | | } |
| | | |
| | | /** |
| | | * 处理 Spring Security 权限不足的异常 |
| | | * |
| | | * 来源是,使用 @PreAuthorize 注解,AOP 进行权限拦截 |