GlobalExceptionHandler.java
8.06 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package com.xly.exception;
import com.xly.constant.ErrorCode;
import com.xly.exception.dto.BusinessException;
import com.xly.tts.bean.TTSResponseDTO;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Order;
import org.springframework.core.Ordered;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Slf4j
@RestControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class GlobalExceptionHandler {
/**
* 处理所有异常
*/
@ExceptionHandler(Exception.class)
public TTSResponseDTO handleException(HttpServletRequest request, Exception e) {
log.error("请求地址: {}, 全局异常: ", request.getRequestURI(), e);
// 获取请求信息
String method = request.getMethod();
String uri = request.getRequestURI();
String queryString = request.getQueryString();
// 记录异常日志(可存入数据库)
// ErrorLog errorLog = ErrorLog.builder()
// .method(method)
// .uri(uri)
// .queryString(queryString)
// .exceptionName(e.getClass().getName())
// .exceptionMessage(e.getMessage())
// .stackTrace(ExceptionUtils.getStackTrace(e))
// .ip(IpUtil.getIpAddr(request))
// .userAgent(request.getHeader("User-Agent"))
// .timestamp(new Date())
// .build();
//
// // 异步保存错误日志
// saveErrorLogAsync(errorLog);
// 生产环境隐藏详细错误信息
if (isProduction()) {
return TTSResponseDTO.error(ErrorCode.SYSTEM_ERROR);
} else {
// 开发环境返回详细错误信息
Map<String, Object> errorDetail = new HashMap<>();
errorDetail.put("exception", e.getClass().getName());
errorDetail.put("message", e.getMessage());
errorDetail.put("path", uri);
errorDetail.put("method", method);
errorDetail.put("timestamp", new Date());
return TTSResponseDTO.error(ErrorCode.SYSTEM_ERROR.getCode(), "系统异常: " + e.getMessage());
}
}
/**
* 处理业务异常
*/
@ExceptionHandler(BusinessException.class)
public TTSResponseDTO handleBusinessException(BusinessException e) {
log.warn("业务异常: {}", e.getMessage());
return TTSResponseDTO.error(e.getCode(), e.getMessage());
}
/**
* 处理数据校验异常
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
public TTSResponseDTO handleMethodArgumentNotValidException(
MethodArgumentNotValidException e) {
List<String> errors = e.getBindingResult().getFieldErrors()
.stream()
.map(error -> error.getField() + ": " + error.getDefaultMessage())
.collect(Collectors.toList());
String message = String.join("; ", errors);
log.warn("参数校验失败: {}", message);
return TTSResponseDTO.error(ErrorCode.PARAM_ERROR.getCode(), message);
}
/**
* 处理约束违反异常
*/
@ExceptionHandler(ConstraintViolationException.class)
public TTSResponseDTO handleConstraintViolationException(
ConstraintViolationException e) {
List<String> errors = e.getConstraintViolations()
.stream()
.map(violation ->
violation.getPropertyPath() + ": " + violation.getMessage())
.collect(Collectors.toList());
String message = String.join("; ", errors);
log.warn("参数约束违反: {}", message);
return TTSResponseDTO.error(ErrorCode.PARAM_ERROR.getCode(), message);
}
/**
* 处理Http请求方法不支持异常
*/
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public TTSResponseDTO handleHttpRequestMethodNotSupportedException(
HttpRequestMethodNotSupportedException e) {
log.warn("HTTP方法不支持: {} {}", e.getMethod(), e.getSupportedHttpMethods());
return TTSResponseDTO.error(ErrorCode.BAD_REQUEST.getCode(),
"请求方法 '" + e.getMethod() + "' 不支持");
}
/**
* 处理媒体类型不支持异常
*/
@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
public TTSResponseDTO handleHttpMediaTypeNotSupportedException(
HttpMediaTypeNotSupportedException e) {
log.warn("媒体类型不支持: {}", e.getContentType());
return TTSResponseDTO.error(ErrorCode.BAD_REQUEST.getCode(),
"媒体类型不支持: " + e.getContentType());
}
/**
* 处理404异常
*/
@ExceptionHandler(NoHandlerFoundException.class)
public TTSResponseDTO handleNoHandlerFoundException(
NoHandlerFoundException e) {
log.warn("接口不存在: {} {}", e.getHttpMethod(), e.getRequestURL());
return TTSResponseDTO.error(ErrorCode.NOT_FOUND.getCode(),
"接口不存在: " + e.getRequestURL());
}
/**
* 处理类型转换异常
*/
@ExceptionHandler(HttpMessageConversionException.class)
public TTSResponseDTO handleHttpMessageConversionException(
HttpMessageConversionException e) {
log.warn("HTTP消息转换异常: {}", e.getMessage());
return TTSResponseDTO.error(ErrorCode.PARAM_ERROR.getCode(),
"参数格式错误");
}
/**
* 处理数据库异常
*/
@ExceptionHandler(DataAccessException.class)
public TTSResponseDTO handleDataAccessException(DataAccessException e) {
log.error("数据库异常: {}", e.getMessage(), e);
// 根据具体异常类型细化处理
if (e instanceof DuplicateKeyException) {
return TTSResponseDTO.error(ErrorCode.DATA_EXISTS.getCode(),
"数据已存在");
} else if (e instanceof DataIntegrityViolationException) {
return TTSResponseDTO.error(ErrorCode.DATA_ERROR.getCode(),
"数据完整性违反");
} else if (e instanceof BadSqlGrammarException) {
return TTSResponseDTO.error(ErrorCode.DB_ERROR.getCode(),
"SQL语法错误");
}
return TTSResponseDTO.error(ErrorCode.DB_ERROR);
}
/**
* 处理JWT异常
*/
// @ExceptionHandler(JwtException.class)
// public TTSResponseDTO handleJwtException(JwtException e) {
// log.warn("JWT异常: {}", e.getMessage());
// return ApiResult.error(ErrorCode.UNAUTHORIZED.getCode(),
// "Token无效或已过期");
// }
// /**
// * 异步保存错误日志
// */
// @Async
// public void saveErrorLogAsync(ErrorLog errorLog) {
// try {
// // 保存到数据库或日志文件
// errorLogService.save(errorLog);
// } catch (Exception ex) {
// log.error("保存错误日志失败", ex);
// }
// }
/***
* @Author 钱豹
* @Date 23:21 2026/1/30
* @Param []
* @return boolean
* @Description 判断是否生产环境还是开发环境 应该根据yml 配置来 后期拓展
**/
private boolean isProduction() {
//TODO
String profile = "dev";
return "prod".equals(profile);
}
}