|
@@ -0,0 +1,132 @@
|
|
1
|
+package com.common.exception;
|
|
2
|
+
|
|
3
|
+import com.alibaba.fastjson.JSONException;
|
|
4
|
+import com.common.utils.LogUtils;
|
|
5
|
+import com.common.utils.R;
|
|
6
|
+import lombok.extern.slf4j.Slf4j;
|
|
7
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
8
|
+import org.springframework.dao.DuplicateKeyException;
|
|
9
|
+import org.springframework.http.converter.HttpMessageConversionException;
|
|
10
|
+import org.springframework.http.converter.HttpMessageNotReadableException;
|
|
11
|
+import org.springframework.validation.FieldError;
|
|
12
|
+import org.springframework.web.HttpRequestMethodNotSupportedException;
|
|
13
|
+import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
14
|
+import org.springframework.web.bind.MissingServletRequestParameterException;
|
|
15
|
+import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
16
|
+import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
17
|
+import org.springframework.web.servlet.NoHandlerFoundException;
|
|
18
|
+
|
|
19
|
+@Slf4j
|
|
20
|
+@RestControllerAdvice
|
|
21
|
+public class RRExceptionHandler {
|
|
22
|
+ @Autowired
|
|
23
|
+ private LogUtils logUtils;
|
|
24
|
+ /**
|
|
25
|
+ * 处理自定义异常
|
|
26
|
+ */
|
|
27
|
+ @ExceptionHandler(RRException.class)
|
|
28
|
+ public R handleRRException(RRException e){
|
|
29
|
+ R r;
|
|
30
|
+// if (StringUtils.isEmpty(e.getMessage())) {
|
|
31
|
+// r = R.error();
|
|
32
|
+// }
|
|
33
|
+ r = R.error(e.getErrno(), e.getErrmsg());
|
|
34
|
+ logUtils.requestLog(r);
|
|
35
|
+ return r;
|
|
36
|
+ }
|
|
37
|
+
|
|
38
|
+ @ExceptionHandler(NoHandlerFoundException.class)
|
|
39
|
+ public R handlerMyNoHandlerFoundException(NoHandlerFoundException e) {
|
|
40
|
+ R r = R.error("404", "路径不存在,请检查路径是否正确");
|
|
41
|
+ logUtils.requestLog(r);
|
|
42
|
+ return r;
|
|
43
|
+ }
|
|
44
|
+
|
|
45
|
+ @ExceptionHandler(DuplicateKeyException.class)
|
|
46
|
+ public R handleDuplicateKeyException(DuplicateKeyException e){
|
|
47
|
+ logUtils.requestLog(e);
|
|
48
|
+ return R.error("数据库中已存在该记录");
|
|
49
|
+ }
|
|
50
|
+
|
|
51
|
+// @ExceptionHandler(AuthorizationException.class)
|
|
52
|
+// public R handleAuthorizationException(AuthorizationException e){
|
|
53
|
+// R r = R.error("没有权限,请联系管理员授权");
|
|
54
|
+// logUtils.requestLog(r);
|
|
55
|
+// return r;
|
|
56
|
+// }
|
|
57
|
+
|
|
58
|
+ /**
|
|
59
|
+ * 处理 @Valid 对 @RequestBody 参数验证异常
|
|
60
|
+ * @param e
|
|
61
|
+ * @return
|
|
62
|
+ */
|
|
63
|
+ @ExceptionHandler(MethodArgumentNotValidException.class)
|
|
64
|
+ public R handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
|
|
65
|
+ FieldError fieldError = e.getBindingResult().getFieldError();
|
|
66
|
+ R r = R.error(fieldError.getField() + fieldError.getDefaultMessage());
|
|
67
|
+ logUtils.requestLog(r);
|
|
68
|
+ return r;
|
|
69
|
+ }
|
|
70
|
+
|
|
71
|
+ /**
|
|
72
|
+ * 参数类型转换错误
|
|
73
|
+ *
|
|
74
|
+ * @param exception 错误
|
|
75
|
+ * @return 错误信息
|
|
76
|
+ */
|
|
77
|
+ @ExceptionHandler(HttpMessageConversionException.class)
|
|
78
|
+ public R parameterTypeException(HttpMessageConversionException exception) {
|
|
79
|
+ R r = R.error(exception.getCause().getLocalizedMessage());
|
|
80
|
+ return r;
|
|
81
|
+ }
|
|
82
|
+
|
|
83
|
+ /**
|
|
84
|
+ * 处理 @RequestBody 但 body none 异常
|
|
85
|
+ * @param e
|
|
86
|
+ * @return
|
|
87
|
+ */
|
|
88
|
+ @ExceptionHandler(HttpMessageNotReadableException.class)
|
|
89
|
+ public R handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
|
|
90
|
+ String message = e.getMessage();
|
|
91
|
+ R r = R.error(message.substring(0, message.indexOf(":")));
|
|
92
|
+ logUtils.requestLog(message);
|
|
93
|
+ return r;
|
|
94
|
+ }
|
|
95
|
+
|
|
96
|
+ /**
|
|
97
|
+ * 处理 @RequestParam required=true 但未收到值异常
|
|
98
|
+ * @param e
|
|
99
|
+ * @return
|
|
100
|
+ */
|
|
101
|
+ @ExceptionHandler(MissingServletRequestParameterException.class)
|
|
102
|
+ public R handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {
|
|
103
|
+ R r = R.error("缺少参数" + e.getParameterName());
|
|
104
|
+ logUtils.requestLog(r);
|
|
105
|
+ return r;
|
|
106
|
+ }
|
|
107
|
+
|
|
108
|
+ @ExceptionHandler(JSONException.class)
|
|
109
|
+ public R handleJSONException(JSONException e){
|
|
110
|
+// logUtils.requestLog(e);
|
|
111
|
+ return R.error("数据格式错误");
|
|
112
|
+ }
|
|
113
|
+
|
|
114
|
+ /**
|
|
115
|
+ * 处理请求方法不支持异常
|
|
116
|
+ * @param e
|
|
117
|
+ * @return
|
|
118
|
+ */
|
|
119
|
+ @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
|
|
120
|
+ public R handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
|
|
121
|
+ R r = R.error(String.format("当前路径不支持%s请求", e.getMethod()));
|
|
122
|
+ logUtils.requestLog(r);
|
|
123
|
+ return r;
|
|
124
|
+ }
|
|
125
|
+
|
|
126
|
+ @ExceptionHandler(Exception.class)
|
|
127
|
+ public R handleException(Exception e){
|
|
128
|
+ logUtils.requestLog(e);
|
|
129
|
+ e.printStackTrace();
|
|
130
|
+ return R.error();
|
|
131
|
+ }
|
|
132
|
+}
|