Просмотр исходного кода

!332 在全局异常拦截器中增加两类异常处理
Merge pull request !332 from OTTO/master

若依 лет назад: 2
Родитель
Сommit
dd338b56da

+ 43 - 31
ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/handler/GlobalExceptionHandler.java

@@ -1,13 +1,5 @@
1
 package com.ruoyi.common.security.handler;
1
 package com.ruoyi.common.security.handler;
2
 
2
 
3
-import javax.servlet.http.HttpServletRequest;
4
-import org.slf4j.Logger;
5
-import org.slf4j.LoggerFactory;
6
-import org.springframework.validation.BindException;
7
-import org.springframework.web.HttpRequestMethodNotSupportedException;
8
-import org.springframework.web.bind.MethodArgumentNotValidException;
9
-import org.springframework.web.bind.annotation.ExceptionHandler;
10
-import org.springframework.web.bind.annotation.RestControllerAdvice;
11
 import com.ruoyi.common.core.constant.HttpStatus;
3
 import com.ruoyi.common.core.constant.HttpStatus;
12
 import com.ruoyi.common.core.exception.DemoModeException;
4
 import com.ruoyi.common.core.exception.DemoModeException;
13
 import com.ruoyi.common.core.exception.InnerAuthException;
5
 import com.ruoyi.common.core.exception.InnerAuthException;
@@ -16,23 +8,32 @@ import com.ruoyi.common.core.exception.auth.NotPermissionException;
16
 import com.ruoyi.common.core.exception.auth.NotRoleException;
8
 import com.ruoyi.common.core.exception.auth.NotRoleException;
17
 import com.ruoyi.common.core.utils.StringUtils;
9
 import com.ruoyi.common.core.utils.StringUtils;
18
 import com.ruoyi.common.core.web.domain.AjaxResult;
10
 import com.ruoyi.common.core.web.domain.AjaxResult;
11
+import org.slf4j.Logger;
12
+import org.slf4j.LoggerFactory;
13
+import org.springframework.validation.BindException;
14
+import org.springframework.web.HttpRequestMethodNotSupportedException;
15
+import org.springframework.web.bind.MethodArgumentNotValidException;
16
+import org.springframework.web.bind.MissingPathVariableException;
17
+import org.springframework.web.bind.annotation.ExceptionHandler;
18
+import org.springframework.web.bind.annotation.RestControllerAdvice;
19
+import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
20
+
21
+import javax.servlet.http.HttpServletRequest;
19
 
22
 
20
 /**
23
 /**
21
  * 全局异常处理器
24
  * 全局异常处理器
22
- * 
25
+ *
23
  * @author ruoyi
26
  * @author ruoyi
24
  */
27
  */
25
 @RestControllerAdvice
28
 @RestControllerAdvice
26
-public class GlobalExceptionHandler
27
-{
29
+public class GlobalExceptionHandler {
28
     private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
30
     private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
29
 
31
 
30
     /**
32
     /**
31
      * 权限码异常
33
      * 权限码异常
32
      */
34
      */
33
     @ExceptionHandler(NotPermissionException.class)
35
     @ExceptionHandler(NotPermissionException.class)
34
-    public AjaxResult handleNotPermissionException(NotPermissionException e, HttpServletRequest request)
35
-    {
36
+    public AjaxResult handleNotPermissionException(NotPermissionException e, HttpServletRequest request) {
36
         String requestURI = request.getRequestURI();
37
         String requestURI = request.getRequestURI();
37
         log.error("请求地址'{}',权限码校验失败'{}'", requestURI, e.getMessage());
38
         log.error("请求地址'{}',权限码校验失败'{}'", requestURI, e.getMessage());
38
         return AjaxResult.error(HttpStatus.FORBIDDEN, "没有访问权限,请联系管理员授权");
39
         return AjaxResult.error(HttpStatus.FORBIDDEN, "没有访问权限,请联系管理员授权");
@@ -42,8 +43,7 @@ public class GlobalExceptionHandler
42
      * 角色权限异常
43
      * 角色权限异常
43
      */
44
      */
44
     @ExceptionHandler(NotRoleException.class)
45
     @ExceptionHandler(NotRoleException.class)
45
-    public AjaxResult handleNotRoleException(NotRoleException e, HttpServletRequest request)
46
-    {
46
+    public AjaxResult handleNotRoleException(NotRoleException e, HttpServletRequest request) {
47
         String requestURI = request.getRequestURI();
47
         String requestURI = request.getRequestURI();
48
         log.error("请求地址'{}',角色权限校验失败'{}'", requestURI, e.getMessage());
48
         log.error("请求地址'{}',角色权限校验失败'{}'", requestURI, e.getMessage());
49
         return AjaxResult.error(HttpStatus.FORBIDDEN, "没有访问权限,请联系管理员授权");
49
         return AjaxResult.error(HttpStatus.FORBIDDEN, "没有访问权限,请联系管理员授权");
@@ -54,8 +54,7 @@ public class GlobalExceptionHandler
54
      */
54
      */
55
     @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
55
     @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
56
     public AjaxResult handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e,
56
     public AjaxResult handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e,
57
-            HttpServletRequest request)
58
-    {
57
+                                                          HttpServletRequest request) {
59
         String requestURI = request.getRequestURI();
58
         String requestURI = request.getRequestURI();
60
         log.error("请求地址'{}',不支持'{}'请求", requestURI, e.getMethod());
59
         log.error("请求地址'{}',不支持'{}'请求", requestURI, e.getMethod());
61
         return AjaxResult.error(e.getMessage());
60
         return AjaxResult.error(e.getMessage());
@@ -65,19 +64,37 @@ public class GlobalExceptionHandler
65
      * 业务异常
64
      * 业务异常
66
      */
65
      */
67
     @ExceptionHandler(ServiceException.class)
66
     @ExceptionHandler(ServiceException.class)
68
-    public AjaxResult handleServiceException(ServiceException e, HttpServletRequest request)
69
-    {
67
+    public AjaxResult handleServiceException(ServiceException e, HttpServletRequest request) {
70
         log.error(e.getMessage(), e);
68
         log.error(e.getMessage(), e);
71
         Integer code = e.getCode();
69
         Integer code = e.getCode();
72
         return StringUtils.isNotNull(code) ? AjaxResult.error(code, e.getMessage()) : AjaxResult.error(e.getMessage());
70
         return StringUtils.isNotNull(code) ? AjaxResult.error(code, e.getMessage()) : AjaxResult.error(e.getMessage());
73
     }
71
     }
74
 
72
 
75
     /**
73
     /**
74
+     * 请求路径中缺少必需的路径变量
75
+     */
76
+    @ExceptionHandler(MissingPathVariableException.class)
77
+    public AjaxResult handleMissingPathVariableException(MissingPathVariableException e, HttpServletRequest request) {
78
+        String requestURI = request.getRequestURI();
79
+        log.error("请求路径中缺少必需的路径变量'{}',发生系统异常.", requestURI, e);
80
+        return AjaxResult.error(String.format("请求路径中缺少必需的路径变量[%s]", e.getVariableName()));
81
+    }
82
+
83
+    /**
84
+     * 请求参数类型不匹配
85
+     */
86
+    @ExceptionHandler(MethodArgumentTypeMismatchException.class)
87
+    public AjaxResult handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e, HttpServletRequest request) {
88
+        String requestURI = request.getRequestURI();
89
+        log.error("请求参数类型不匹配'{}',发生系统异常.", requestURI, e);
90
+        return AjaxResult.error(String.format("请求参数类型不匹配,参数[%s]要求类型为:'%s',但输入值为:'%s'", e.getName(), e.getRequiredType().getName(), e.getValue()));
91
+    }
92
+
93
+    /**
76
      * 拦截未知的运行时异常
94
      * 拦截未知的运行时异常
77
      */
95
      */
78
     @ExceptionHandler(RuntimeException.class)
96
     @ExceptionHandler(RuntimeException.class)
79
-    public AjaxResult handleRuntimeException(RuntimeException e, HttpServletRequest request)
80
-    {
97
+    public AjaxResult handleRuntimeException(RuntimeException e, HttpServletRequest request) {
81
         String requestURI = request.getRequestURI();
98
         String requestURI = request.getRequestURI();
82
         log.error("请求地址'{}',发生未知异常.", requestURI, e);
99
         log.error("请求地址'{}',发生未知异常.", requestURI, e);
83
         return AjaxResult.error(e.getMessage());
100
         return AjaxResult.error(e.getMessage());
@@ -87,8 +104,7 @@ public class GlobalExceptionHandler
87
      * 系统异常
104
      * 系统异常
88
      */
105
      */
89
     @ExceptionHandler(Exception.class)
106
     @ExceptionHandler(Exception.class)
90
-    public AjaxResult handleException(Exception e, HttpServletRequest request)
91
-    {
107
+    public AjaxResult handleException(Exception e, HttpServletRequest request) {
92
         String requestURI = request.getRequestURI();
108
         String requestURI = request.getRequestURI();
93
         log.error("请求地址'{}',发生系统异常.", requestURI, e);
109
         log.error("请求地址'{}',发生系统异常.", requestURI, e);
94
         return AjaxResult.error(e.getMessage());
110
         return AjaxResult.error(e.getMessage());
@@ -98,8 +114,7 @@ public class GlobalExceptionHandler
98
      * 自定义验证异常
114
      * 自定义验证异常
99
      */
115
      */
100
     @ExceptionHandler(BindException.class)
116
     @ExceptionHandler(BindException.class)
101
-    public AjaxResult handleBindException(BindException e)
102
-    {
117
+    public AjaxResult handleBindException(BindException e) {
103
         log.error(e.getMessage(), e);
118
         log.error(e.getMessage(), e);
104
         String message = e.getAllErrors().get(0).getDefaultMessage();
119
         String message = e.getAllErrors().get(0).getDefaultMessage();
105
         return AjaxResult.error(message);
120
         return AjaxResult.error(message);
@@ -109,8 +124,7 @@ public class GlobalExceptionHandler
109
      * 自定义验证异常
124
      * 自定义验证异常
110
      */
125
      */
111
     @ExceptionHandler(MethodArgumentNotValidException.class)
126
     @ExceptionHandler(MethodArgumentNotValidException.class)
112
-    public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e)
113
-    {
127
+    public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
114
         log.error(e.getMessage(), e);
128
         log.error(e.getMessage(), e);
115
         String message = e.getBindingResult().getFieldError().getDefaultMessage();
129
         String message = e.getBindingResult().getFieldError().getDefaultMessage();
116
         return AjaxResult.error(message);
130
         return AjaxResult.error(message);
@@ -120,8 +134,7 @@ public class GlobalExceptionHandler
120
      * 内部认证异常
134
      * 内部认证异常
121
      */
135
      */
122
     @ExceptionHandler(InnerAuthException.class)
136
     @ExceptionHandler(InnerAuthException.class)
123
-    public AjaxResult handleInnerAuthException(InnerAuthException e)
124
-    {
137
+    public AjaxResult handleInnerAuthException(InnerAuthException e) {
125
         return AjaxResult.error(e.getMessage());
138
         return AjaxResult.error(e.getMessage());
126
     }
139
     }
127
 
140
 
@@ -129,8 +142,7 @@ public class GlobalExceptionHandler
129
      * 演示模式异常
142
      * 演示模式异常
130
      */
143
      */
131
     @ExceptionHandler(DemoModeException.class)
144
     @ExceptionHandler(DemoModeException.class)
132
-    public AjaxResult handleDemoModeException(DemoModeException e)
133
-    {
145
+    public AjaxResult handleDemoModeException(DemoModeException e) {
134
         return AjaxResult.error("演示模式,不允许操作");
146
         return AjaxResult.error("演示模式,不允许操作");
135
     }
147
     }
136
 }
148
 }