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