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
package com.talentcard.server.config;
 
import com.talentcard.server.business.admin.BenefitServiceCouponAdminController;
import com.talentcard.server.common.ApiResponse;
import com.talentcard.server.common.BusinessException;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
 
@Order(Ordered.HIGHEST_PRECEDENCE)
@RestControllerAdvice(assignableTypes = BenefitServiceCouponAdminController.class)
public class BenefitServiceCouponExceptionHandler {
    @ExceptionHandler(BusinessException.class)
    public ResponseEntity<ApiResponse<Void>> business(BusinessException exception) {
        return error(exception.getMessage());
    }
 
    @ExceptionHandler(Exception.class)
    public ResponseEntity<ApiResponse<Void>> exception(Exception exception) {
        return error("优惠券配置处理失败");
    }
 
    private ResponseEntity<ApiResponse<Void>> error(String message) {
        String fallback = "优惠券配置处理失败";
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .body(ApiResponse.failure(500, message == null || message.isBlank() ? fallback : message));
    }
}