fix(web): 修复全局异常处理器并更新版本号

- 添加 ValidationException 异常处理支持
- 扩展异常处理器以捕获 RuntimeException
- 为 BindException 添加文档注释
- 新增 ValidationException 专门处理方法
- 更新父项目及所有子模块版本从 1.5.2 到 1.5.3
This commit is contained in:
zkh
2025-12-31 11:53:04 +08:00
parent a0cb0cb6b7
commit eeac5b430c
7 changed files with 18 additions and 8 deletions

View File

@ -1,5 +1,6 @@
package vip.jcfd.web.config;
import jakarta.validation.ValidationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
@ -18,8 +19,8 @@ public class GlobalExceptionHandler {
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@ExceptionHandler(value = Exception.class)
public R<String> handleException(Exception e) {
@ExceptionHandler(value = {Exception.class, RuntimeException.class})
public R<String> handleException(Throwable e) {
log.error("服务异常", e);
return R.serverError("服务器繁忙,请稍候重试");
}
@ -37,6 +38,9 @@ public class GlobalExceptionHandler {
}
/**
* Handles bind exceptions; logs and returns formatted field errors
*/
@ExceptionHandler(value = BindException.class)
public R<String> handleBindException(BindException e) {
log.error("接口入参校验失败", e);
@ -44,4 +48,10 @@ public class GlobalExceptionHandler {
List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
return R.error(String.join("\n", fieldErrors.stream().map(FieldError::getDefaultMessage).toList()));
}
@ExceptionHandler(value = ValidationException.class)
public R<String> handleValidationException(ValidationException e) {
log.error("接口入参校验失败", e);
return R.error(e.getMessage());
}
}