GlobalExceptionHandler.java
4.41 KB
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
package com.diligrp.tax.central.config;
import com.diligrp.tax.central.exception.TaxAgentServiceException;
import com.diligrp.tax.central.message.Message;
import com.diligrp.tax.central.type.TaxSystemType;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
* 统一异常处理器
*/
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
/**
* 处理业务异常
*/
@ExceptionHandler(TaxAgentServiceException.class)
public Message<?> handleBusinessException(TaxAgentServiceException e) {
log.error("Method throw TaxAgentServiceException", e);
return Message.failure(e.getCode(), e.getMessage());
}
/**
* 统一拦截参数转换异常 JSON -> Object
*
* @param ex InvalidFormatException
* @return 响应消息对象
*/
@ExceptionHandler({HttpMessageNotReadableException.class, InvalidFormatException.class})
public Message<?> invalidFormatExceptionHandler(Exception ex) {
return Message.failure(TaxSystemType.ABNORMAL_PARAMETERS.code, ex.getMessage());
}
/**
* 参数验证
*
* @param exs 参数异常
* @return BaseOutput
*/
@ExceptionHandler(value = ConstraintViolationException.class)
public Message<?> constraintViolationException(ConstraintViolationException exs) {
Set<ConstraintViolation<?>> violations = exs.getConstraintViolations();
String msg = violations.stream().map(ConstraintViolation::getMessage)
.collect(Collectors.joining(","));
return Message.failure(TaxSystemType.ABNORMAL_PARAMETERS.code, msg);
}
/**
* 参数验证
*
* @param e MethodArgumentNotValidException @RequestBody
* BindException 普通form表单
* @return BaseOutput
*/
@ExceptionHandler(value = {MethodArgumentNotValidException.class, BindException.class})
public Message<?> methodArgumentNotValidException(Exception e) {
BindingResult bindingResult;
if (e instanceof MethodArgumentNotValidException) {
bindingResult = ((MethodArgumentNotValidException) e).getBindingResult();
} else {
bindingResult = ((BindException) e).getBindingResult();
}
List<ObjectError> allErrors = bindingResult.getAllErrors();
String msg = allErrors.stream().map(DefaultMessageSourceResolvable::getDefaultMessage)
.collect(Collectors.joining("、"));
return Message.failure(TaxSystemType.ABNORMAL_PARAMETERS.code, msg);
}
/**
* 缺少请求参数异常
*
* @param ex 请求参数异常
* @return 异常信息
*/
@ExceptionHandler(MissingServletRequestParameterException.class)
public Message<?> missingServletRequestParameterExceptionHandler(MissingServletRequestParameterException ex) {
return Message.failure(TaxSystemType.ABNORMAL_PARAMETERS.code, String.format("缺少参数:%s", ex.getParameterName()));
}
/**
* 断言验证
*
* @param ex IllegalArgumentException
* @return BaseOutput
*/
@ExceptionHandler(value = IllegalArgumentException.class)
public Message<?> illegalArgumentException(IllegalArgumentException ex) {
return Message.failure(TaxSystemType.ABNORMAL_PARAMETERS.code, ex.getMessage());
}
/**
* 统一拦截系统其它异常
*
* @param ex 系统异常
* @return 响应消息对象
*/
@ExceptionHandler(Exception.class)
public Message<?> exceptionHandler(Exception ex) {
log.error("unknown error :", ex);
return Message.failure(TaxSystemType.UNKNOWN_ERROR.code, TaxSystemType.UNKNOWN_ERROR.message);
}
}