GlobalExceptionHandler.java
2.28 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
package com.xly.erp.common.exception;
import com.xly.erp.common.response.ErrorCode;
import com.xly.erp.common.response.Result;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
* 全局异常处理器。
* 把 BizException / 参数校验异常 / 兜底异常转 Result.fail 统一响应。
* docs/04 § 1.4。
*/
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
@ExceptionHandler(BizException.class)
public ResponseEntity<Result<Object>> handleBiz(BizException e) {
log.warn("[BizException] code={} message={} hasData={}", e.getCode(), e.getMessage(), e.getData() != null);
Result<Object> body = e.getData() != null
? Result.fail(e.getCode(), e.getMessage(), e.getData())
: Result.fail(e.getCode(), e.getMessage());
return ResponseEntity
.status(ErrorCode.toHttpStatus(e.getCode()))
.body(body);
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<Result<Void>> handleValidation(MethodArgumentNotValidException e) {
String msg = e.getBindingResult().getFieldErrors().stream()
.findFirst()
.map(fe -> fe.getField() + " " + fe.getDefaultMessage())
.orElse("参数校验失败");
return ResponseEntity
.status(400)
.body(Result.fail(ErrorCode.BAD_REQUEST, msg));
}
@ExceptionHandler(ConstraintViolationException.class)
public ResponseEntity<Result<Void>> handleConstraint(ConstraintViolationException e) {
return ResponseEntity
.status(400)
.body(Result.fail(ErrorCode.BAD_REQUEST, e.getMessage()));
}
@ExceptionHandler(Exception.class)
public ResponseEntity<Result<Void>> handleFallback(Exception e) {
log.error("[Unhandled] {}", e.getMessage(), e);
return ResponseEntity
.status(500)
.body(Result.fail(ErrorCode.INTERNAL_ERROR, "服务器内部错误"));
}
}