GlobalExceptionHandler.java
4.78 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
package com.xly.exception;
import com.xly.constant.ErrorCode;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
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.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
private Map<String, Object> error(Integer code, String message) {
Map<String, Object> body = new LinkedHashMap<>();
body.put("code", code);
body.put("message", message);
body.put("timestamp", System.currentTimeMillis());
return body;
}
@ExceptionHandler(Exception.class)
public Map<String, Object> handleException(HttpServletRequest request, Exception e) {
log.error("请求地址: {}, 全局异常: ", request.getRequestURI(), e);
return error(ErrorCode.SYSTEM_ERROR.getCode(), "系统异常: " + e.getMessage());
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public Map<String, Object> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
List<String> errors = e.getBindingResult().getFieldErrors()
.stream()
.map(err -> err.getField() + ": " + err.getDefaultMessage())
.collect(Collectors.toList());
String message = String.join("; ", errors);
log.warn("参数校验失败: {}", message);
return error(ErrorCode.PARAM_ERROR.getCode(), message);
}
@ExceptionHandler(ConstraintViolationException.class)
public Map<String, Object> 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 error(ErrorCode.PARAM_ERROR.getCode(), message);
}
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public Map<String, Object> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
log.warn("HTTP方法不支持: {} {}", e.getMethod(), e.getSupportedHttpMethods());
return error(ErrorCode.BAD_REQUEST.getCode(), "请求方法 '" + e.getMethod() + "' 不支持");
}
@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
public Map<String, Object> handleHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException e) {
log.warn("媒体类型不支持: {}", e.getContentType());
return error(ErrorCode.BAD_REQUEST.getCode(), "媒体类型不支持: " + e.getContentType());
}
@ExceptionHandler(NoHandlerFoundException.class)
public Map<String, Object> handleNoHandlerFoundException(NoHandlerFoundException e) {
log.warn("接口不存在: {} {}", e.getHttpMethod(), e.getRequestURL());
return error(ErrorCode.NOT_FOUND.getCode(), "接口不存在: " + e.getRequestURL());
}
@ExceptionHandler(HttpMessageConversionException.class)
public Map<String, Object> handleHttpMessageConversionException(HttpMessageConversionException e) {
log.warn("HTTP消息转换异常: {}", e.getMessage());
return error(ErrorCode.PARAM_ERROR.getCode(), "参数格式错误");
}
@ExceptionHandler(DataAccessException.class)
public Map<String, Object> handleDataAccessException(DataAccessException e) {
log.error("数据库异常: {}", e.getMessage(), e);
if (e instanceof DuplicateKeyException) {
return error(ErrorCode.DATA_EXISTS.getCode(), "数据已存在");
} else if (e instanceof DataIntegrityViolationException) {
return error(ErrorCode.DATA_ERROR.getCode(), "数据完整性违反");
} else if (e instanceof BadSqlGrammarException) {
return error(ErrorCode.DB_ERROR.getCode(), "SQL语法错误");
}
return error(ErrorCode.DB_ERROR.getCode(), ErrorCode.DB_ERROR.getMessage());
}
}