Quellcode durchsuchen

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

若依 vor 2 Jahren
Ursprung
Commit
dd338b56da

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

@@ -1,13 +1,5 @@
1 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 3
 import com.ruoyi.common.core.constant.HttpStatus;
12 4
 import com.ruoyi.common.core.exception.DemoModeException;
13 5
 import com.ruoyi.common.core.exception.InnerAuthException;
@@ -16,23 +8,32 @@ import com.ruoyi.common.core.exception.auth.NotPermissionException;
16 8
 import com.ruoyi.common.core.exception.auth.NotRoleException;
17 9
 import com.ruoyi.common.core.utils.StringUtils;
18 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 26
  * @author ruoyi
24 27
  */
25 28
 @RestControllerAdvice
26
-public class GlobalExceptionHandler
27
-{
29
+public class GlobalExceptionHandler {
28 30
     private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
29 31
 
30 32
     /**
31 33
      * 权限码异常
32 34
      */
33 35
     @ExceptionHandler(NotPermissionException.class)
34
-    public AjaxResult handleNotPermissionException(NotPermissionException e, HttpServletRequest request)
35
-    {
36
+    public AjaxResult handleNotPermissionException(NotPermissionException e, HttpServletRequest request) {
36 37
         String requestURI = request.getRequestURI();
37 38
         log.error("请求地址'{}',权限码校验失败'{}'", requestURI, e.getMessage());
38 39
         return AjaxResult.error(HttpStatus.FORBIDDEN, "没有访问权限,请联系管理员授权");
@@ -42,8 +43,7 @@ public class GlobalExceptionHandler
42 43
      * 角色权限异常
43 44
      */
44 45
     @ExceptionHandler(NotRoleException.class)
45
-    public AjaxResult handleNotRoleException(NotRoleException e, HttpServletRequest request)
46
-    {
46
+    public AjaxResult handleNotRoleException(NotRoleException e, HttpServletRequest request) {
47 47
         String requestURI = request.getRequestURI();
48 48
         log.error("请求地址'{}',角色权限校验失败'{}'", requestURI, e.getMessage());
49 49
         return AjaxResult.error(HttpStatus.FORBIDDEN, "没有访问权限,请联系管理员授权");
@@ -54,8 +54,7 @@ public class GlobalExceptionHandler
54 54
      */
55 55
     @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
56 56
     public AjaxResult handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e,
57
-            HttpServletRequest request)
58
-    {
57
+                                                          HttpServletRequest request) {
59 58
         String requestURI = request.getRequestURI();
60 59
         log.error("请求地址'{}',不支持'{}'请求", requestURI, e.getMethod());
61 60
         return AjaxResult.error(e.getMessage());
@@ -65,19 +64,37 @@ public class GlobalExceptionHandler
65 64
      * 业务异常
66 65
      */
67 66
     @ExceptionHandler(ServiceException.class)
68
-    public AjaxResult handleServiceException(ServiceException e, HttpServletRequest request)
69
-    {
67
+    public AjaxResult handleServiceException(ServiceException e, HttpServletRequest request) {
70 68
         log.error(e.getMessage(), e);
71 69
         Integer code = e.getCode();
72 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 96
     @ExceptionHandler(RuntimeException.class)
79
-    public AjaxResult handleRuntimeException(RuntimeException e, HttpServletRequest request)
80
-    {
97
+    public AjaxResult handleRuntimeException(RuntimeException e, HttpServletRequest request) {
81 98
         String requestURI = request.getRequestURI();
82 99
         log.error("请求地址'{}',发生未知异常.", requestURI, e);
83 100
         return AjaxResult.error(e.getMessage());
@@ -87,8 +104,7 @@ public class GlobalExceptionHandler
87 104
      * 系统异常
88 105
      */
89 106
     @ExceptionHandler(Exception.class)
90
-    public AjaxResult handleException(Exception e, HttpServletRequest request)
91
-    {
107
+    public AjaxResult handleException(Exception e, HttpServletRequest request) {
92 108
         String requestURI = request.getRequestURI();
93 109
         log.error("请求地址'{}',发生系统异常.", requestURI, e);
94 110
         return AjaxResult.error(e.getMessage());
@@ -98,8 +114,7 @@ public class GlobalExceptionHandler
98 114
      * 自定义验证异常
99 115
      */
100 116
     @ExceptionHandler(BindException.class)
101
-    public AjaxResult handleBindException(BindException e)
102
-    {
117
+    public AjaxResult handleBindException(BindException e) {
103 118
         log.error(e.getMessage(), e);
104 119
         String message = e.getAllErrors().get(0).getDefaultMessage();
105 120
         return AjaxResult.error(message);
@@ -109,8 +124,7 @@ public class GlobalExceptionHandler
109 124
      * 自定义验证异常
110 125
      */
111 126
     @ExceptionHandler(MethodArgumentNotValidException.class)
112
-    public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e)
113
-    {
127
+    public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
114 128
         log.error(e.getMessage(), e);
115 129
         String message = e.getBindingResult().getFieldError().getDefaultMessage();
116 130
         return AjaxResult.error(message);
@@ -120,8 +134,7 @@ public class GlobalExceptionHandler
120 134
      * 内部认证异常
121 135
      */
122 136
     @ExceptionHandler(InnerAuthException.class)
123
-    public AjaxResult handleInnerAuthException(InnerAuthException e)
124
-    {
137
+    public AjaxResult handleInnerAuthException(InnerAuthException e) {
125 138
         return AjaxResult.error(e.getMessage());
126 139
     }
127 140
 
@@ -129,8 +142,7 @@ public class GlobalExceptionHandler
129 142
      * 演示模式异常
130 143
      */
131 144
     @ExceptionHandler(DemoModeException.class)
132
-    public AjaxResult handleDemoModeException(DemoModeException e)
133
-    {
145
+    public AjaxResult handleDemoModeException(DemoModeException e) {
134 146
         return AjaxResult.error("演示模式,不允许操作");
135 147
     }
136 148
 }