From ecca9ab70a9a87bcb60977c92fbf81053b8fc1bb Mon Sep 17 00:00:00 2001
From: mitao <2763622819@qq.com>
Date: 星期三, 04 九月 2024 09:17:55 +0800
Subject: [PATCH] 新增优化

---
 ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/handler/GlobalExceptionHandler.java |   28 ++++++++++++++++------------
 1 files changed, 16 insertions(+), 12 deletions(-)

diff --git a/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/handler/GlobalExceptionHandler.java b/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/handler/GlobalExceptionHandler.java
index bbaef82..23d684c 100644
--- a/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/handler/GlobalExceptionHandler.java
+++ b/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/handler/GlobalExceptionHandler.java
@@ -13,6 +13,7 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.validation.BindException;
+import org.springframework.validation.FieldError;
 import org.springframework.web.HttpRequestMethodNotSupportedException;
 import org.springframework.web.bind.MethodArgumentNotValidException;
 import org.springframework.web.bind.MissingPathVariableException;
@@ -122,16 +123,6 @@
     }
 
     /**
-     * 自定义验证异常
-     */
-    @ExceptionHandler(MethodArgumentNotValidException.class)
-    public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
-        log.error(e.getMessage(), e);
-        String message = e.getBindingResult().getFieldError().getDefaultMessage();
-        return AjaxResult.error(message);
-    }
-
-    /**
      * 内部认证异常
      */
     @ExceptionHandler(InnerAuthException.class)
@@ -148,11 +139,24 @@
     }
 
     /**
-     * 演示模式异常
+     * 自定义异常
      */
     @ExceptionHandler(GlobalException.class)
     public R<String> handleDemoModeException(GlobalException g) {
-        return R.fail(g.getDetailMessage());
+        return R.fail(null, g.getCode() == null ? 500 : g.getCode(), g.getMessage());
+    }
+
+    /**
+     * 捕获jsr303参数校验异常
+     *
+     * @author hjl
+     */
+    @ExceptionHandler(MethodArgumentNotValidException.class)
+    public R<String> methodArgumentNotValidExceptionExceptionException(MethodArgumentNotValidException e) {
+        FieldError fieldError = e.getBindingResult().getFieldError();
+        assert fieldError != null;
+        String defaultMessage = fieldError.getDefaultMessage();
+        return R.fail(defaultMessage);
     }
 
 }

--
Gitblit v1.7.1