카드 매입 승인 경로 Spring Boot 3 전환 (ACX-E2E-1785394564819)
Some checks are pending
Verify / source-contract (push) Waiting to run
Some checks are pending
Verify / source-contract (push) Waiting to run
This commit is contained in:
parent
acc4062cbf
commit
e9cd885700
1 changed files with 74 additions and 0 deletions
|
|
@ -0,0 +1,74 @@
|
||||||
|
package com.acquirex.exception;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.validation.FieldError;
|
||||||
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 전역 예외 처리 핸들러 - Spring Boot 3 (jakarta.validation)
|
||||||
|
*/
|
||||||
|
@RestControllerAdvice
|
||||||
|
@Slf4j
|
||||||
|
public class GlobalExceptionHandler {
|
||||||
|
|
||||||
|
@ExceptionHandler(AcquisitionNotFoundException.class)
|
||||||
|
public ResponseEntity<Map<String, Object>> handleAcquisitionNotFound(AcquisitionNotFoundException ex) {
|
||||||
|
log.error("매입 정보 없음: {}", ex.getMessage());
|
||||||
|
return buildErrorResponse(HttpStatus.NOT_FOUND, ex.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(InvalidAcquisitionStateException.class)
|
||||||
|
public ResponseEntity<Map<String, Object>> handleInvalidAcquisitionState(InvalidAcquisitionStateException ex) {
|
||||||
|
log.error("유효하지 않은 매입 상태: {}", ex.getMessage());
|
||||||
|
return buildErrorResponse(HttpStatus.BAD_REQUEST, ex.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||||
|
public ResponseEntity<Map<String, Object>> handleValidationErrors(MethodArgumentNotValidException ex) {
|
||||||
|
Map<String, String> errors = new HashMap<>();
|
||||||
|
ex.getBindingResult().getAllErrors().forEach(error -> {
|
||||||
|
String fieldName = ((FieldError) error).getField();
|
||||||
|
errors.put(fieldName, error.getDefaultMessage());
|
||||||
|
});
|
||||||
|
log.error("검증 오류: {}", errors);
|
||||||
|
Map<String, Object> response = new HashMap<>();
|
||||||
|
response.put("timestamp", LocalDateTime.now());
|
||||||
|
response.put("status", HttpStatus.BAD_REQUEST.value());
|
||||||
|
response.put("error", "Validation Failed");
|
||||||
|
response.put("errors", errors);
|
||||||
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(Exception.class)
|
||||||
|
public ResponseEntity<Map<String, Object>> handleGenericException(Exception ex) {
|
||||||
|
log.error("예상치 못한 오류 발생", ex);
|
||||||
|
return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, "예상치 못한 오류가 발생했습니다");
|
||||||
|
}
|
||||||
|
|
||||||
|
private ResponseEntity<Map<String, Object>> buildErrorResponse(HttpStatus status, String message) {
|
||||||
|
Map<String, Object> response = new HashMap<>();
|
||||||
|
response.put("timestamp", LocalDateTime.now());
|
||||||
|
response.put("status", status.value());
|
||||||
|
response.put("error", status.getReasonPhrase());
|
||||||
|
response.put("message", message);
|
||||||
|
return ResponseEntity.status(status).body(response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 매입 정보를 찾을 수 없을 때 발생하는 예외 */
|
||||||
|
class AcquisitionNotFoundException extends RuntimeException {
|
||||||
|
public AcquisitionNotFoundException(String message) { super(message); }
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 매입 상태가 유효하지 않을 때 발생하는 예외 */
|
||||||
|
class InvalidAcquisitionStateException extends RuntimeException {
|
||||||
|
public InvalidAcquisitionStateException(String message) { super(message); }
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue