|
@@ -0,0 +1,67 @@
|
|
1
|
+package com.xhkjedu.sclass.config;
|
|
2
|
+import com.alibaba.fastjson.JSON;
|
|
3
|
+import com.xhkjedu.config.MissingParametersException;
|
|
4
|
+import com.xhkjedu.utils.N_Utils;
|
|
5
|
+import com.xhkjedu.vo.ResultVo;
|
|
6
|
+import lombok.extern.slf4j.Slf4j;
|
|
7
|
+import org.springframework.dao.DataIntegrityViolationException;
|
|
8
|
+import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
9
|
+import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
10
|
+import org.springframework.web.bind.annotation.ResponseBody;
|
|
11
|
+
|
|
12
|
+import javax.servlet.http.HttpServletRequest;
|
|
13
|
+
|
|
14
|
+/**
|
|
15
|
+ * @创建人 :zj
|
|
16
|
+ * @创建时间 2019/11/7
|
|
17
|
+ * @修改人和其它信息
|
|
18
|
+ * @描述 :项目异常处理类:
|
|
19
|
+ * 项目中所有controller层抛出的异常信息都会被拦截。
|
|
20
|
+ * 方便我们查看异常原因,减少开发中大量的try catch。
|
|
21
|
+ * 拦截异常后,打印异常信息,返回前端约定参数对象。
|
|
22
|
+ */
|
|
23
|
+@Slf4j
|
|
24
|
+@ResponseBody
|
|
25
|
+@ControllerAdvice
|
|
26
|
+public class ErrorAdivceController {
|
|
27
|
+ @ExceptionHandler(MissingParametersException.class)
|
|
28
|
+ public ResultVo missingParametersException(MissingParametersException ex) {
|
|
29
|
+ log.error("参数异常: -------> " + ex.getMessage());
|
|
30
|
+ return new ResultVo(1, ex.getMessage());
|
|
31
|
+ }
|
|
32
|
+
|
|
33
|
+ @ExceptionHandler(DataIntegrityViolationException.class)
|
|
34
|
+ public ResultVo DataIntegrityViolationException(DataIntegrityViolationException ex){
|
|
35
|
+ log.error("数据库异常: -------> " + ex.getMessage());
|
|
36
|
+ String msg = ex.getMessage();
|
|
37
|
+ if (N_Utils.isNotEmpty(msg) && msg.indexOf("SQLIntegrityConstraintViolationException") != -1){
|
|
38
|
+ return new ResultVo(1,"存在关联信息");
|
|
39
|
+ }
|
|
40
|
+ return new ResultVo(1,"数据库处理异常");
|
|
41
|
+ }
|
|
42
|
+
|
|
43
|
+ @ExceptionHandler(Exception.class)
|
|
44
|
+ public ResultVo exceptionHandler(Exception ex) {
|
|
45
|
+ log.error("程序运行异常: -------> " + ex.getMessage());
|
|
46
|
+ String msg = ex.getMessage();
|
|
47
|
+ if (N_Utils.isNotEmpty(msg) && msg.indexOf("JSON parse error") != -1){
|
|
48
|
+ return new ResultVo(1,"json格式不对");
|
|
49
|
+ }
|
|
50
|
+ return new ResultVo(1, "程序处理异常");
|
|
51
|
+ }
|
|
52
|
+
|
|
53
|
+ @ExceptionHandler(NullPointerException.class)
|
|
54
|
+ public ResultVo nullPointerException(NullPointerException ex) {
|
|
55
|
+ StackTraceElement element = ex.getStackTrace()[0];
|
|
56
|
+ log.error("参数为空异常: -------> " + JSON.toJSONString(element));
|
|
57
|
+ return new ResultVo(1, "参数为空异常");
|
|
58
|
+ }
|
|
59
|
+
|
|
60
|
+ @ExceptionHandler(value = IllegalArgumentException.class)
|
|
61
|
+ public ResultVo illegalArgumentExceptionHandler(HttpServletRequest request,IllegalArgumentException ex) {
|
|
62
|
+ log.error("请求参数错误 URL : {},Exception : {}", request.getRequestURL(),ex.getMessage());
|
|
63
|
+ return new ResultVo(1, "请求参数错误");
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+}
|