yeg
2026-09-17 20e19d8693d08ccb47e6533d8523fd44cd546a70
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
package com.talentcard.server.config;
 
import java.util.stream.Collectors;
 
import com.talentcard.server.business.admin.CarWashJumpConfigAdminController;
import com.talentcard.server.business.admin.CarWashBenefitAdminController;
import com.talentcard.server.business.portal.CarWashJumpConfigPortalController;
import com.talentcard.server.business.portal.CarWashCredentialPortalController;
import com.talentcard.server.common.ApiResponse;
import com.talentcard.server.common.BusinessException;
import jakarta.validation.ConstraintViolationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
 
@Order(Ordered.HIGHEST_PRECEDENCE)
@RestControllerAdvice(assignableTypes = {
        CarWashJumpConfigAdminController.class,
        CarWashBenefitAdminController.class,
        CarWashJumpConfigPortalController.class,
        CarWashCredentialPortalController.class
})
public class CarWashJumpConfigExceptionHandler {
    private static final Logger log = LoggerFactory.getLogger(CarWashJumpConfigExceptionHandler.class);
    private static final int ERROR_CODE = 500;
 
    @ExceptionHandler(BusinessException.class)
    public ResponseEntity<ApiResponse<Void>> handleBusinessException(BusinessException exception) {
        return error(exception.getMessage());
    }
 
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity<ApiResponse<Void>> handleMethodArgumentNotValid(MethodArgumentNotValidException exception) {
        String message = exception.getBindingResult().getFieldErrors().stream()
                .map(error -> error.getDefaultMessage() == null ? error.getField() + "参数不正确" : error.getDefaultMessage())
                .collect(Collectors.joining(";"));
        return error(message);
    }
 
    @ExceptionHandler(BindException.class)
    public ResponseEntity<ApiResponse<Void>> handleBindException(BindException exception) {
        String message = exception.getBindingResult().getFieldErrors().stream()
                .map(error -> error.getDefaultMessage() == null ? error.getField() + "参数不正确" : error.getDefaultMessage())
                .collect(Collectors.joining(";"));
        return error(message);
    }
 
    @ExceptionHandler(ConstraintViolationException.class)
    public ResponseEntity<ApiResponse<Void>> handleConstraintViolation(ConstraintViolationException exception) {
        String message = exception.getConstraintViolations().stream()
                .map(violation -> violation.getMessage())
                .collect(Collectors.joining(";"));
        return error(message);
    }
 
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public ResponseEntity<ApiResponse<Void>> handleUnreadableMessage(HttpMessageNotReadableException exception) {
        return error("请求内容格式不正确");
    }
 
    @ExceptionHandler(MethodArgumentTypeMismatchException.class)
    public ResponseEntity<ApiResponse<Void>> handleTypeMismatch(MethodArgumentTypeMismatchException exception) {
        return error("请求参数格式不正确:" + exception.getName());
    }
 
    @ExceptionHandler(Exception.class)
    public ResponseEntity<ApiResponse<Void>> handleException(Exception exception) {
        log.error("Car wash jump config request exception", exception);
        return error("服务器内部错误");
    }
 
    private ResponseEntity<ApiResponse<Void>> error(String message) {
        String safeMessage = message == null || message.isBlank() ? "服务器内部错误" : message;
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .body(ApiResponse.failure(ERROR_CODE, safeMessage));
    }
}