Ver código fonte

style(在全局异常拦截器中增加两类异常处理): 格式化代码

otto 2 anos atrás
pai
commit
0647ca8b66

+ 22 - 32
ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/handler/GlobalExceptionHandler.java

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