|
@@ -18,6 +18,8 @@ import jakarta.validation.ValidationException;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.eco.vip.orm.exception.BusinessException;
|
|
|
import org.eco.vip.orm.pojo.CommonResult;
|
|
|
+import org.eco.vip.orm.utils.StrUtils;
|
|
|
+import org.mybatis.spring.MyBatisSystemException;
|
|
|
import org.springframework.validation.FieldError;
|
|
|
import org.springframework.validation.ObjectError;
|
|
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
@@ -40,14 +42,25 @@ import static org.eco.vip.orm.exception.enums.GlobalErrorCode.NOT_FOUND;
|
|
|
@Slf4j
|
|
|
public class GlobalExceptionHandler {
|
|
|
|
|
|
+ /**
|
|
|
+ * 认证失败
|
|
|
+ */
|
|
|
+ @ExceptionHandler(NotLoginException.class)
|
|
|
+ public CommonResult<Void> handleNotLoginException(NotLoginException loginException, HttpServletRequest request) {
|
|
|
+ log.warn("[handleNotLoginException]", loginException);
|
|
|
+ String uri = request.getRequestURI();
|
|
|
+ log.error("请求地址'{}',认证失败'{}',无法访问系统资源", uri, loginException.getMessage());
|
|
|
+ return CommonResult.fail(HttpStatus.HTTP_UNAUTHORIZED, "认证失败,无法访问系统资源");
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 业务异常
|
|
|
*/
|
|
|
@ExceptionHandler(BusinessException.class)
|
|
|
- public CommonResult<Void> handleServiceException(BusinessException e) {
|
|
|
- log.error(e.getMessage());
|
|
|
- Integer code = e.getCode();
|
|
|
- return ObjectUtil.isNotNull(code) ? CommonResult.fail(code, e.getMessage()) : CommonResult.fail(e.getMessage());
|
|
|
+ public CommonResult<Void> handleBusinessException(BusinessException exception, HttpServletRequest request) {
|
|
|
+ log.warn("[handleBusinessException]", exception);
|
|
|
+ Integer code = exception.getCode();
|
|
|
+ return ObjectUtil.isNotNull(code) ? CommonResult.fail(code, exception.getMessage()) : CommonResult.fail(exception.getMessage());
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -76,7 +89,7 @@ public class GlobalExceptionHandler {
|
|
|
* 处理 Validator 校验不通过产生的异常
|
|
|
*/
|
|
|
@ExceptionHandler(value = ConstraintViolationException.class)
|
|
|
- public CommonResult<?> constraintViolationExceptionHandler(ConstraintViolationException exception) {
|
|
|
+ public CommonResult<?> constraintViolationExceptionHandler(ConstraintViolationException exception, HttpServletRequest request) {
|
|
|
log.warn("[constraintViolationExceptionHandler]", exception);
|
|
|
ConstraintViolation<?> constraintViolation = exception.getConstraintViolations().iterator().next();
|
|
|
return CommonResult.fail(BAD_REQUEST.getCode(), String.format("请求参数不正确:%s", constraintViolation.getMessage()));
|
|
@@ -86,7 +99,7 @@ public class GlobalExceptionHandler {
|
|
|
* 处理 Dubbo Consumer 本地参数校验时,抛出的 ValidationException 异常
|
|
|
*/
|
|
|
@ExceptionHandler(value = ValidationException.class)
|
|
|
- public CommonResult<?> validationException(ValidationException exception) {
|
|
|
+ public CommonResult<?> validationException(ValidationException exception, HttpServletRequest request) {
|
|
|
log.warn("[validationException]", exception);
|
|
|
// 无法拼接明细的错误信息,因为 Dubbo Consumer 抛出 ValidationException 异常时,是直接的字符串信息,且人类不可读
|
|
|
return CommonResult.fail(BAD_REQUEST);
|
|
@@ -96,22 +109,11 @@ public class GlobalExceptionHandler {
|
|
|
* 处理 SpringMVC 请求地址不存在
|
|
|
*/
|
|
|
@ExceptionHandler(NoResourceFoundException.class)
|
|
|
- private CommonResult<?> noResourceFoundExceptionHandler(HttpServletRequest req, NoResourceFoundException throwable) {
|
|
|
+ private CommonResult<?> noResourceFoundExceptionHandler(NoResourceFoundException throwable, HttpServletRequest request) {
|
|
|
log.warn("[noResourceFoundExceptionHandler]", throwable);
|
|
|
return CommonResult.fail(NOT_FOUND.getCode(), String.format("请求地址不存在:%s", throwable.getResourcePath()));
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 认证失败
|
|
|
- */
|
|
|
- @ExceptionHandler(NotLoginException.class)
|
|
|
- public CommonResult<Void> handleNotLoginException(NotLoginException loginException, HttpServletRequest request) {
|
|
|
- log.warn("[handleNotLoginException]", loginException);
|
|
|
- String uri = request.getRequestURI();
|
|
|
- log.error("请求地址'{}',认证失败'{}',无法访问系统资源", uri, loginException.getMessage());
|
|
|
- return CommonResult.fail(HttpStatus.HTTP_UNAUTHORIZED, "认证失败,无法访问系统资源");
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 系统异常
|
|
|
*/
|
|
@@ -123,15 +125,19 @@ public class GlobalExceptionHandler {
|
|
|
log.error("请求地址'{}',发生系统异常.{}", requestUri, message);
|
|
|
return CommonResult.fail(message);
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
- * 系统异常
|
|
|
+ * Mybatis系统异常 通用处理
|
|
|
*/
|
|
|
- @ExceptionHandler(Exception.class)
|
|
|
- public CommonResult<Void> handleRuntimeException(RuntimeException runtimeException, HttpServletRequest request) {
|
|
|
- log.warn("[handleRuntimeException]", runtimeException);
|
|
|
+ @ExceptionHandler(MyBatisSystemException.class)
|
|
|
+ public CommonResult<Void> handleCannotFindDataSourceException(MyBatisSystemException exception, HttpServletRequest request) {
|
|
|
String requestUri = request.getRequestURI();
|
|
|
- String message = ExceptionUtil.getRootCauseMessage(runtimeException);
|
|
|
- log.error("请求地址'{}',发生未知异常.{}", requestUri, message);
|
|
|
+ String message = ExceptionUtil.getRootCauseMessage(exception);
|
|
|
+ if (StrUtils.contains("CannotFindDataSourceException", message)) {
|
|
|
+ log.error("请求地址'{}', 未找到数据源", requestUri);
|
|
|
+ return CommonResult.fail("未找到数据源,请联系管理员确认");
|
|
|
+ }
|
|
|
+ log.error("请求地址'{}', Mybatis系统异常", requestUri, exception);
|
|
|
return CommonResult.fail(message);
|
|
|
}
|
|
|
}
|