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
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
package com.gsc4cloud.gsc.facade.vpcminiapp.handler;
import com.gsc4cloud.gsc.common.core.util.R;
import com.gsc4cloud.gsc.vpc.api.enums.ErrorCodeEnum;
import com.gsc4cloud.gsc.vpc.api.exception.BusinessException;
import feign.FeignException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
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 java.util.Optional;
import java.util.stream.Collectors;
/**
* @Description 本地异常处理类
* @Author yelq
* @Date 2025/9/19-20:47
* @Version 1.0
*/
@Order(9999)
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
public GlobalExceptionHandler() {
}
/**
* 处理业务异常
*/
@ExceptionHandler(BusinessException.class)
public ResponseEntity<R<?>> handleBusinessException(BusinessException e) {
log.error("业务异常: code={}, message={}", e.getCode(), e.getMessage(), e);
return ResponseEntity
.status(HttpStatus.OK)
.body(R.restResult(null, e.getCode(), e.getMessage()));
}
/**
* 参数校验异常统一处理
*/
@ExceptionHandler({MethodArgumentNotValidException.class, BindException.class})
public ResponseEntity<R<?>> handleValidationException(Exception e) {
BindingResult bindingResult = null;
if (e instanceof MethodArgumentNotValidException) {
bindingResult = ((MethodArgumentNotValidException) e).getBindingResult();
} else if (e instanceof BindException) {
bindingResult = ((BindException) e).getBindingResult();
}
String message = Optional.ofNullable(bindingResult)
.map(result -> result.getAllErrors().stream()
.map(ObjectError::getDefaultMessage)
.collect(Collectors.joining("; ")))
.orElse("参数校验失败");
log.error("参数校验异常: {}", message, e);
return ResponseEntity
.status(HttpStatus.OK)
.body(R.restResult(null, ErrorCodeEnum.PARAM_INVALID.getCode(), message));
}
/**
* 处理请求方法不支持异常
*/
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public ResponseEntity<R<?>> handleHttpRequestMethodNotSupportedException(
HttpRequestMethodNotSupportedException e) {
log.error("请求方法不支持: {}", e.getMessage(), e);
return ResponseEntity
.status(HttpStatus.METHOD_NOT_ALLOWED)
.body(R.restResult(null,405, "请求方法不支持"));
}
/**
* 处理媒体类型不支持异常
*/
@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
public ResponseEntity<R<?>> handleHttpMediaTypeNotSupportedException(
HttpMediaTypeNotSupportedException e) {
log.error("媒体类型不支持: {}", e.getMessage(), e);
return ResponseEntity
.status(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
.body(R.restResult(null,415, "媒体类型不支持"));
}
/**
* 处理空指针异常
*/
@ExceptionHandler(NullPointerException.class)
public ResponseEntity<R<?>> handleNullPointerException(NullPointerException e) {
log.error("空指针异常", e);
return ResponseEntity
.status(HttpStatus.OK)
.body(R.restResult(null, ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMessage()));
}
/**
* 处理其他所有异常
*/
@ExceptionHandler(Exception.class)
public ResponseEntity<R<?>> handleException(Exception e) {
log.error("系统异常", e);
return ResponseEntity
.status(HttpStatus.OK)
.body(R.restResult(null, ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMessage()));
}
/**
* 处理Feign客户端异常
*/
@ExceptionHandler(FeignException.class)
public ResponseEntity<R<?>> handleFeignException(FeignException e) {
log.error("Feign客户端异常: status={}, message={}", e.status(), e.getMessage(), e);
return ResponseEntity
.status(HttpStatus.OK)
.body(R.restResult(null, ErrorCodeEnum.FEIGN_ERROR.getCode(),"服务调用失败: " + e.getMessage()));
}
}