“djh”
2024-12-05 eee41a5fb58e6547a43929430f4b72908119db6e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package com.gkhy.testFourierSpecialGasMonitor.config.exception;
 
 
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gkhy.testFourierSpecialGasMonitor.commons.domain.ForeignResult;
import com.gkhy.testFourierSpecialGasMonitor.commons.domain.Result;
import com.gkhy.testFourierSpecialGasMonitor.commons.enums.ResultCode;
import com.gkhy.testFourierSpecialGasMonitor.commons.exception.BusinessException;
import com.gkhy.testFourierSpecialGasMonitor.commons.exception.DataReceiveException;
import com.gkhy.testFourierSpecialGasMonitor.commons.exception.ExceptionInfo;
import com.gkhy.testFourierSpecialGasMonitor.commons.exception.RepeatedClickException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
 
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
 
@ControllerAdvice
public class GlobalExceptionHandler {
 
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
 
    @Autowired
    private ObjectMapper objectMapper;
 
    /**
     * 通用异常
     */
    @ResponseBody
    @ExceptionHandler(value = BusinessException.class)
    public Result businessExceptionHandler(BusinessException e) throws JsonProcessingException {
        ExceptionInfo exceptionInfo = new ExceptionInfo();
        exceptionInfo.setTime(LocalDateTime.now());
        exceptionInfo.setCode(e.getCode());
        exceptionInfo.setMsg(e.getMessage());
        exceptionInfo.setCauseClass(e.getCauseClass());
        logger.error(objectMapper.writeValueAsString(exceptionInfo));
        Result result = new Result();
        result.setCode(e.getCode());
        if(e.getMessage() == null || e.getMessage().isEmpty()){
            ResultCode code = ResultCode.prase(e.getCode());
            if(code != null)
                result.setMsg(code.getDesc());
        }else {
            result.setMsg(e.getMessage());
        }
        return result;
    }
 
    /**
     * 通用异常
     */
    @ResponseBody
    @ExceptionHandler(value = DataReceiveException.class)
    public ForeignResult dataReceiveExceptionHandler(DataReceiveException e) throws JsonProcessingException {
        ExceptionInfo exceptionInfo = new ExceptionInfo();
        LocalDateTime now = LocalDateTime.now();
        exceptionInfo.setTime(now);
        exceptionInfo.setCode(e.getCode());
        exceptionInfo.setMsg(e.getMessage());
        exceptionInfo.setCauseClass(e.getCauseClass());
        logger.error(objectMapper.writeValueAsString(exceptionInfo));
        ForeignResult result = new ForeignResult<>();
        result.setCode(e.getCode());
        result.setTime(now.format(formatter));
        if(e.getMessage() == null || e.getMessage().isEmpty()){
            ResultCode code = ResultCode.prase(e.getCode());
            if(code != null)
                result.setData(code.getDesc());
        }else {
            result.setData(e.getMessage());
        }
        return result;
    }
 
    /**
     * 重复点击异常
     */
    @ResponseBody
    @ExceptionHandler(value = RepeatedClickException.class)
    public Result repeatedClickExceptionHandler(RepeatedClickException e) throws JsonProcessingException {
        Result result = new Result();
        result.setSuccess();
        result.setMsg(e.getMessage());
        return result;
    }
 
    /**
    * @Description: AuthenticationException
    */
 
    @ResponseBody
    @ExceptionHandler(value = AuthenticationException.class)
    public Result CHandler(AuthenticationException e) {
        logger.warn(e.getMessage());
        return new Result(ResultCode.BUSINESS_ERROR_PERMISSION_DENIALED);
    }
 
 
    /**
     * @Description: AuthenticationException
     */
 
    @ResponseBody
    @ExceptionHandler(value = AccessDeniedException.class)
    public Result DHandler(AccessDeniedException e) {
        logger.warn(e.getMessage());
        return new Result(ResultCode.BUSINESS_ERROR_PERMISSION_DENIALED);
 
    }
 
    /**
    * @Description: 请求方法
    */
    @ResponseBody
    @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
    public Result DHandler(HttpRequestMethodNotSupportedException e) {
        Result resultVO = new Result();
        resultVO.setCode(ResultCode.BUSINESS_ERROR_HTTP_METHOD_NOT_SUPPORT.getCode());
        resultVO.setMsg(e.getMessage());
        return resultVO;
    }
 
 
    /**
    * @Description: @RequestBody 请求体解析问题
    */
    @ResponseBody
    @ExceptionHandler(value = HttpMessageNotReadableException.class)
    public Result DHandler(HttpMessageNotReadableException e) {
        Result resultVO = new Result();
        resultVO.setCode(ResultCode.PARAM_ERROR_ILLEGAL.getCode());
        resultVO.setMsg(e.getMessage());
        return resultVO;
 
    }
 
 
 
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Result errorHandler(Exception e) {
        e.printStackTrace();
        logger.error(e.getMessage());
        Result resultVO = new Result();
        resultVO.setCode(ResultCode.SYSTEM_ERROR.getCode());
        return resultVO;
//        return new ResultVO(ResultCodes.SERVER_ERROR);
    }
 
    /**
     * 处理入参异常
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result handleMethodArgumentNotValidException(MethodArgumentNotValidException e){
        logger.warn(e.getBindingResult().getFieldError().getDefaultMessage());
        return new Result(ResultCode.PARAM_ERROR,
                e.getBindingResult().getFieldError().getDefaultMessage());
    }
 
}