|
@@ -0,0 +1,87 @@
|
|
|
+/*
|
|
|
+ * Copyright (c) 2025 GaoKunW
|
|
|
+ *
|
|
|
+ */
|
|
|
+
|
|
|
+package org.eco.vip.web.config.core.handler;
|
|
|
+
|
|
|
+
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.exceptions.ExceptionUtil;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import jakarta.servlet.http.HttpServletRequest;
|
|
|
+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 java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @description GlobalExceptionHandler 全局异常处理器
|
|
|
+ *
|
|
|
+ * @author GaoKunW
|
|
|
+ * @date 2025/7/1 15:24
|
|
|
+ */
|
|
|
+@RestControllerAdvice
|
|
|
+@Slf4j
|
|
|
+public class GlobalExceptionHandler {
|
|
|
+
|
|
|
+ public CommonResult<Void> allExceptionHandler(HttpServletRequest request, Throwable throwable) {
|
|
|
+ if (throwable instanceof MethodArgumentNotValidException) {
|
|
|
+ return handleMethodArgumentNotValidException((MethodArgumentNotValidException) throwable);
|
|
|
+ }
|
|
|
+
|
|
|
+ return defaultExceptionHandler(request, throwable);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 业务异常
|
|
|
+ */
|
|
|
+ @ExceptionHandler(BusinessException.class)
|
|
|
+ public CommonResult<Void> handleServiceException(BusinessException e, HttpServletRequest request) {
|
|
|
+ log.error(e.getMessage());
|
|
|
+ Integer code = e.getCode();
|
|
|
+ return ObjectUtil.isNotNull(code) ? CommonResult.fail(code, e.getMessage()) : CommonResult.fail(e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 自定义Valid验证异常
|
|
|
+ */
|
|
|
+ @ExceptionHandler(MethodArgumentNotValidException.class)
|
|
|
+ public CommonResult<Void> handleMethodArgumentNotValidException(MethodArgumentNotValidException exception) {
|
|
|
+ log.warn("[handleMethodArgumentNotValidException]", exception);
|
|
|
+ String errorMessage = null;
|
|
|
+ FieldError fieldError = exception.getBindingResult().getFieldError();
|
|
|
+ if (ObjectUtil.isNull(fieldError)) {
|
|
|
+ List<ObjectError> allErrors = exception.getBindingResult().getAllErrors();
|
|
|
+ if(CollUtil.isNotEmpty(allErrors)) {
|
|
|
+ errorMessage = allErrors.getFirst().getDefaultMessage();
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ errorMessage = fieldError.getDefaultMessage();
|
|
|
+ }
|
|
|
+ if (StrUtil.isEmpty(errorMessage)) {
|
|
|
+ return CommonResult.fail(GlobalErrorCode.BAD_REQUEST);
|
|
|
+ }
|
|
|
+ return CommonResult.fail(GlobalErrorCode.BAD_REQUEST.getCode(), String.format("请求参数不正确:%s", errorMessage));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 系统异常
|
|
|
+ */
|
|
|
+ @ExceptionHandler(Exception.class)
|
|
|
+ public CommonResult<Void> defaultExceptionHandler(HttpServletRequest request, Throwable throwable) {
|
|
|
+ String requestUri = request.getRequestURI();
|
|
|
+ String message = ExceptionUtil.getRootCauseMessage(throwable);
|
|
|
+ log.error("请求地址'{}',发生系统异常.{}", requestUri, message);
|
|
|
+ return CommonResult.fail(message);
|
|
|
+ }
|
|
|
+}
|