yeg
2 天以前 d8eaa16682cf180e9563d6be05fb43057daf6c07
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
package com.talentcard.server.common;
 
import java.util.stream.Collectors;
 
import jakarta.validation.ConstraintViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
@RestControllerAdvice
public class GlobalExceptionHandler {
 
    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
 
    @ExceptionHandler(BusinessException.class)
    public ResponseEntity<ApiResponse<Void>> handleBusinessException(BusinessException exception) {
        HttpStatus status = HttpStatus.valueOf(exception.errorCode().code());
        return ResponseEntity.status(status)
                .body(ApiResponse.failure(exception.errorCode().code(), exception.getMessage()));
    }
 
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity<ApiResponse<Void>> handleMethodArgumentNotValid(MethodArgumentNotValidException exception) {
        String message = exception.getBindingResult().getFieldErrors().stream()
                .map(error -> error.getField() + " " + error.getDefaultMessage())
                .collect(Collectors.joining("; "));
        return badRequest(message);
    }
 
    @ExceptionHandler(BindException.class)
    public ResponseEntity<ApiResponse<Void>> handleBindException(BindException exception) {
        String message = exception.getBindingResult().getFieldErrors().stream()
                .map(error -> error.getField() + " " + error.getDefaultMessage())
                .collect(Collectors.joining("; "));
        return badRequest(message);
    }
 
    @ExceptionHandler(ConstraintViolationException.class)
    public ResponseEntity<ApiResponse<Void>> handleConstraintViolation(ConstraintViolationException exception) {
        String message = exception.getConstraintViolations().stream()
                .map(violation -> violation.getPropertyPath() + " " + violation.getMessage())
                .collect(Collectors.joining("; "));
        return badRequest(message);
    }
 
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public ResponseEntity<ApiResponse<Void>> handleUnreadableMessage(HttpMessageNotReadableException exception) {
        return badRequest("请求内容格式不正确");
    }
 
    /**
     * 权限拦截器用 {@link ResponseStatusException} 表达 403;不显式处理会被下面的兜底分支包成 500,
     * 前端只看到「服务器内部错误」,看不出是权限问题。这里原样透出状态码和原因。
     */
    @ExceptionHandler(ResponseStatusException.class)
    public ResponseEntity<ApiResponse<Void>> handleResponseStatus(ResponseStatusException exception) {
        int status = exception.getStatusCode().value();
        String reason = exception.getReason() == null || exception.getReason().isBlank()
                ? "请求被拒绝" : exception.getReason();
        return ResponseEntity.status(status).body(ApiResponse.failure(status, reason));
    }
 
    /**
     * 唯一键冲突(例如同一微信换绑并发、卡号/证件号重复)以前会掉进下面的兜底分支,前端只看到
     * 「服务器内部错误」,用户除了反复重试没有别的办法。这里按约束名给出可读提示,返回 409。
     */
    @ExceptionHandler(DuplicateKeyException.class)
    public ResponseEntity<ApiResponse<Void>> handleDuplicateKey(DuplicateKeyException exception) {
        Throwable cause = exception.getMostSpecificCause();
        log.warn("Duplicate key conflict: {}", cause == null ? exception.getMessage() : cause.getMessage());
        return ResponseEntity.status(HttpStatus.CONFLICT)
                .body(ApiResponse.failure(HttpStatus.CONFLICT.value(), duplicateKeyMessage(exception)));
    }
 
    private String duplicateKeyMessage(DuplicateKeyException exception) {
        Throwable cause = exception.getMostSpecificCause();
        String detail = cause == null ? "" : String.valueOf(cause.getMessage());
        if (detail.contains("uk_openid")) return "该微信已绑定到其它账号,请稍后重试或联系管理员处理";
        if (detail.contains("uk_mobile")) return "该手机号已存在账号,请改用原手机号登录或联系管理员";
        // 先判 id_card:'uk_id_card_no' 里也含 'card_no',顺序反了会把它当成卡号。
        if (detail.contains("id_card")) return "该证件号已存在账号,请核对后重试";
        if (detail.contains("card_no")) return "该人才卡号已被使用,请核对后重试";
        return "数据已存在,请核对后重试";
    }
 
    @ExceptionHandler(Exception.class)
    public ResponseEntity<ApiResponse<Void>> handleException(Exception exception) {
        log.error("Unhandled request exception", exception);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .body(ApiResponse.failure(ErrorCode.INTERNAL_ERROR.code(), ErrorCode.INTERNAL_ERROR.message()));
    }
 
    private ResponseEntity<ApiResponse<Void>> badRequest(String message) {
        return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                .body(ApiResponse.failure(ErrorCode.BAD_REQUEST.code(), message));
    }
}