|
@@ -11,18 +11,24 @@ import cn.hutool.core.exceptions.ExceptionUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
|
+import jakarta.validation.ConstraintViolation;
|
|
|
+import jakarta.validation.ConstraintViolationException;
|
|
|
+import jakarta.validation.ValidationException;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.eco.vip.orm.exception.BusinessException;
|
|
|
-import org.eco.vip.orm.exception.enums.GlobalErrorCode;
|
|
|
import org.eco.vip.orm.pojo.CommonResult;
|
|
|
import org.springframework.validation.FieldError;
|
|
|
import org.springframework.validation.ObjectError;
|
|
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
+import org.springframework.web.servlet.resource.NoResourceFoundException;
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
+import static org.eco.vip.orm.exception.enums.GlobalErrorCode.BAD_REQUEST;
|
|
|
+import static org.eco.vip.orm.exception.enums.GlobalErrorCode.NOT_FOUND;
|
|
|
+
|
|
|
/**
|
|
|
* @description GlobalExceptionHandler 全局异常处理器
|
|
|
*
|
|
@@ -33,10 +39,19 @@ import java.util.List;
|
|
|
@Slf4j
|
|
|
public class GlobalExceptionHandler {
|
|
|
|
|
|
- public CommonResult<Void> allExceptionHandler(HttpServletRequest request, Throwable throwable) {
|
|
|
+ public CommonResult<?> allExceptionHandler(HttpServletRequest request, Throwable throwable) {
|
|
|
if (throwable instanceof MethodArgumentNotValidException) {
|
|
|
return handleMethodArgumentNotValidException((MethodArgumentNotValidException) throwable);
|
|
|
}
|
|
|
+ if (throwable instanceof ConstraintViolationException) {
|
|
|
+ return constraintViolationExceptionHandler((ConstraintViolationException) throwable);
|
|
|
+ }
|
|
|
+ if (throwable instanceof ValidationException) {
|
|
|
+ return validationException((ValidationException) throwable);
|
|
|
+ }
|
|
|
+ if (throwable instanceof NoResourceFoundException) {
|
|
|
+ return noResourceFoundExceptionHandler(request, (NoResourceFoundException) throwable);
|
|
|
+ }
|
|
|
return defaultExceptionHandler(request, throwable);
|
|
|
}
|
|
|
|
|
@@ -67,9 +82,38 @@ public class GlobalExceptionHandler {
|
|
|
errorMessage = fieldError.getDefaultMessage();
|
|
|
}
|
|
|
if (StrUtil.isEmpty(errorMessage)) {
|
|
|
- return CommonResult.fail(GlobalErrorCode.BAD_REQUEST);
|
|
|
+ return CommonResult.fail(BAD_REQUEST);
|
|
|
}
|
|
|
- return CommonResult.fail(GlobalErrorCode.BAD_REQUEST.getCode(), String.format("请求参数不正确:%s", errorMessage));
|
|
|
+ return CommonResult.fail(BAD_REQUEST.getCode(), String.format("请求参数不正确:%s", errorMessage));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理 Validator 校验不通过产生的异常
|
|
|
+ */
|
|
|
+ @ExceptionHandler(value = ConstraintViolationException.class)
|
|
|
+ public CommonResult<?> constraintViolationExceptionHandler(ConstraintViolationException exception) {
|
|
|
+ log.warn("[constraintViolationExceptionHandler]", exception);
|
|
|
+ ConstraintViolation<?> constraintViolation = exception.getConstraintViolations().iterator().next();
|
|
|
+ return CommonResult.fail(BAD_REQUEST.getCode(), String.format("请求参数不正确:%s", constraintViolation.getMessage()));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理 Dubbo Consumer 本地参数校验时,抛出的 ValidationException 异常
|
|
|
+ */
|
|
|
+ @ExceptionHandler(value = ValidationException.class)
|
|
|
+ public CommonResult<?> validationException(ValidationException exception) {
|
|
|
+ log.warn("[validationException]", exception);
|
|
|
+ // 无法拼接明细的错误信息,因为 Dubbo Consumer 抛出 ValidationException 异常时,是直接的字符串信息,且人类不可读
|
|
|
+ return CommonResult.fail(BAD_REQUEST);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理 SpringMVC 请求地址不存在
|
|
|
+ */
|
|
|
+ @ExceptionHandler(NoResourceFoundException.class)
|
|
|
+ private CommonResult<?> noResourceFoundExceptionHandler(HttpServletRequest req, NoResourceFoundException throwable) {
|
|
|
+ log.warn("[noResourceFoundExceptionHandler]", throwable);
|
|
|
+ return CommonResult.fail(NOT_FOUND.getCode(), String.format("请求地址不存在:%s", throwable.getResourcePath()));
|
|
|
}
|
|
|
|
|
|
/**
|