guang
2023-04-24 6044ac660b623034eaf0d7d8512aff99463458d1
提交 | 用户 | age
9e1c8d 1 package com.hx.other.service.config;
G 2
3 import com.fasterxml.jackson.databind.exc.InvalidFormatException;
4 import com.hx.exception.ParamException;
5 import com.hx.exception.ServiceException;
6 import com.hx.exception.TipsException;
7 import com.hx.resultTool.ResponseCode;
8 import com.hx.resultTool.Result;
9 import lombok.extern.slf4j.Slf4j;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12 import org.springframework.http.converter.HttpMessageNotReadableException;
13 import org.springframework.jdbc.BadSqlGrammarException;
14 import org.springframework.validation.BindException;
15 import org.springframework.validation.BindingResult;
16 import org.springframework.web.bind.MethodArgumentNotValidException;
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.ServletException;
22 import java.io.FileNotFoundException;
23 import java.io.IOException;
24
25 /**
26  * 统一异常处理中心
27  *
28  * @Author: cmg
29  * @Date: 2023/4/23 10:29
30  */
31 @RestControllerAdvice
32 @Slf4j
33 public class GlobalExceptionHandler {
34
35     /**
36      * 参数校验错误异常
37      *
38      * @param e MethodArgumentNotValidException|BindException
39      * @return ResponseData
40      */
41     @ExceptionHandler(value = {BindException.class, MethodArgumentNotValidException.class})
42     public Result bindExceptionHandler(Exception e) {
43         BindingResult bindingResult;
44         if (e instanceof BindException) {
45             bindingResult = ((BindException) e).getBindingResult();
46         } else {
47             bindingResult = ((MethodArgumentNotValidException) e).getBindingResult();
48         }
49         StringBuilder stringBuilder = new StringBuilder();
50         bindingResult.getAllErrors().forEach(
51                 objectError ->
52                         stringBuilder.append(",").append(objectError.getDefaultMessage())
53         );
54         String errorMessage = stringBuilder.toString();
55         errorMessage = errorMessage.substring(1, errorMessage.length());
56         log.error(getExceptionInformation(e));
57         return Result.failure(ResponseCode.ERROR_PARAMS_VALIDATOR,errorMessage);
58     }
59
60     /**
61      * 捕获统一提示异常
62      *
63      * @param e AbstractGlobalException
64      * @return ResponseData
65      */
66     @ExceptionHandler(value = TipsException.class)
67     public Result customExceptionHandler(TipsException e) {
68         //和爷要求删除
69 //        e.printStackTrace();
70 //        logger.error(getExceptionInformation(e));
71         return Result.failure(ResponseCode.ERROR_TIPS,e.getMessage());
72     }
73
74     /**
75      * 捕获统一业务/判断异常
76      *
77      * @param e AbstractGlobalException
78      * @return ResponseData
79      */
80     @ExceptionHandler(value = ServiceException.class)
81     public Result customExceptionHandler(ServiceException e) {
82         e.printStackTrace();
83         log.error(getExceptionInformation(e));
84         return Result.failure(ResponseCode.ERROR_SERVICE_VALIDATOR,e.getMessage());
85     }
86
87     /**
88      * 捕获统一业务/判断异常
89      *
90      * @param e AbstractGlobalException
91      * @return ResponseData
92      */
93     @ExceptionHandler(value = ParamException.class)
94     public Result customExceptionHandler(ParamException e) {
95         //e.printStackTrace();
96         //logger.error(getExceptionInformation(e));
97         return Result.failure(ResponseCode.ERROR_PARAMS_VALIDATOR,e.getMessage());
98     }
99
100     /**
101      * 捕获body空报错异常和入参类型异常
102      *
103      * @param e AbstractGlobalException
104      * @return ResponseData
105      */
106     @ExceptionHandler(value = HttpMessageNotReadableException.class)
107     public Result customHttpMessageNotReadableException(HttpMessageNotReadableException e) {
108         if(e.getCause() instanceof InvalidFormatException){
109             //入参类型错误
110             return Result.failure(ResponseCode.ERROR_PARAMS_VALIDATOR,"参数类型错误:"+((InvalidFormatException) e.getCause()).getOriginalMessage());
111         }
112         return Result.failure(ResponseCode.ERROR_TIPS,"传参结构不能为空!");
113     }
114
115     /**
116      * 捕获未知异常
117      *
118      * @param e Exception
119      * @return ResponseData
120      */
121     @ExceptionHandler(value = Exception.class)
122     public Result commonExceptionHandler(Exception e) throws ServletException {
123         String errMsg = "系统错误!";
124         if (e instanceof ServletException) {
125             throw (ServletException) e;
126         }else if(e instanceof ArrayIndexOutOfBoundsException){
127             errMsg = "系统错误(sizeErr)!";
128         }else if(e instanceof FileNotFoundException){
129             errMsg = "系统错误(fileNot)!";
130         }else if(e instanceof IOException){
131             errMsg = "系统错误(IO)!";
132         }else if(e instanceof NullPointerException){
133             errMsg = "系统错误(null)!";
134         }else if(e instanceof BadSqlGrammarException){
135             errMsg = "系统错误(sql)!";
136         }else if(e instanceof MethodArgumentTypeMismatchException){
137             errMsg = "系统错误(参数类型错误)!";
138         } else if (e instanceof HttpMessageNotReadableException){
139             errMsg = "参数格式错误01!";
140         }else if (e.getCause() instanceof InvalidFormatException){
141             errMsg = "参数格式错误02!";
142         }
143         log.error(getExceptionInformation(e));
144         return Result.failure(ResponseCode.ERROR_SYSTEM, errMsg);
145     }
146
147     /**
148      * 获取异常信息
149      * @param ex
150      * @return
151      */
152     public static String getExceptionInformation(Exception ex){
153         String sOut = ex.getMessage() + "\r\n";
154         StackTraceElement[] trace = ex.getStackTrace();
155         for (StackTraceElement s : trace) {
156             sOut += "\tat " + s + "\r\n";
157         }
158         return sOut;
159     };
160 }