wenminjie
2026-08-05 652b229d0a675f5bfdf297d5f116fe9698ff2725
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
package com.talentcard.server.config;
 
import com.talentcard.server.business.admin.DependentCardApplyAdminController;
import com.talentcard.server.common.ApiResponse;
import com.talentcard.server.common.BusinessException;
import jakarta.validation.ConstraintViolationException;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
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;
 
@Order(Ordered.HIGHEST_PRECEDENCE)
@RestControllerAdvice(assignableTypes = DependentCardApplyAdminController.class)
public class DependentCardApplyAdminExceptionHandler {
    @ExceptionHandler(BusinessException.class)
    public ResponseEntity<ApiResponse<Void>> handleBusinessException(BusinessException exception) {
        return error(exception.getMessage());
    }
 
    @ExceptionHandler({MethodArgumentNotValidException.class, BindException.class,
            ConstraintViolationException.class, HttpMessageNotReadableException.class})
    public ResponseEntity<ApiResponse<Void>> handleRequestException(Exception exception) {
        return error("\u5ba1\u6838\u53c2\u6570\u4e0d\u6b63\u786e");
    }
 
    @ExceptionHandler(Exception.class)
    public ResponseEntity<ApiResponse<Void>> handleException(Exception exception) {
        return error("\u5bb6\u5c5e\u9644\u5c5e\u5361\u7533\u8bf7\u5904\u7406\u5931\u8d25");
    }
 
    private ResponseEntity<ApiResponse<Void>> error(String message) {
        String fallback = "\u5bb6\u5c5e\u9644\u5c5e\u5361\u7533\u8bf7\u5904\u7406\u5931\u8d25";
        String safeMessage = message == null || message.isBlank() ? fallback : message;
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .body(ApiResponse.failure(500, safeMessage));
    }
}