|
|
@@ -6,6 +6,7 @@ import jakarta.servlet.http.HttpServletRequest;
|
|
|
import jakarta.validation.ConstraintViolation;
|
|
|
import jakarta.validation.ConstraintViolationException;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.mybatis.spring.MyBatisSystemException;
|
|
|
import org.springframework.dao.DataAccessException;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.validation.BindingResult;
|
|
|
@@ -14,6 +15,7 @@ import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
+import org.springframework.web.servlet.resource.NoResourceFoundException;
|
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
@@ -67,12 +69,27 @@ public class GlobalExceptionHandler {
|
|
|
* 场景:查询时序库时SQL错误、连接超时等
|
|
|
*/
|
|
|
@ExceptionHandler({DataAccessException.class, java.sql.SQLException.class})
|
|
|
- public BaseResult<Void> handleDbException(Exception e, HttpServletRequest request) {
|
|
|
+ public BaseResult<Void> handleDbException(DataAccessException e, HttpServletRequest request) {
|
|
|
// 打印异常日志
|
|
|
log.warn("数据库异常,请求路径:{}, 原因:{}", request.getRequestURI(), e.getMessage(), e);
|
|
|
return BaseResult.error(ErrorCodeEnum.DB_OPERATE_FAIL.getCode(), "数据库操作失败,请稍后重试");
|
|
|
}
|
|
|
|
|
|
+ // 处理 MyBatis 数据库相关异常
|
|
|
+ @ExceptionHandler(MyBatisSystemException.class)
|
|
|
+ public BaseResult<Void> handleMyBatisException(MyBatisSystemException e, HttpServletRequest request) {
|
|
|
+ log.warn("数据库操作失败,请求路径:{}, 原因:{}", request.getRequestURI(), e.getMessage(), e);
|
|
|
+ return BaseResult.error(ErrorCodeEnum.DB_OPERATE_FAIL.getCode(), "数据库操作失败,请稍后重试");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理静态资源未找到异常
|
|
|
+ @ExceptionHandler(NoResourceFoundException.class)
|
|
|
+ public BaseResult<Void> handleNoResourceException(NoResourceFoundException e, HttpServletRequest request) {
|
|
|
+ log.warn("请求的资源不存在,请求路径:{}, 原因:{}", request.getRequestURI(), e.getMessage(), e);
|
|
|
+ return BaseResult.error(ErrorCodeEnum.DB_OPERATE_FAIL.getCode(), "请求的资源不存在,请稍后重试");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 处理系统通用异常(未被上述方法捕获的异常)
|
|
|
* 场景:空指针、数组越界等未知错误
|