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));
|
}
|
}
|