|
| 1 | +package com.bootsignal.global.exception; |
| 2 | + |
| 3 | +import com.bootsignal.global.response.ApiResponse; |
| 4 | +import com.bootsignal.global.response.ErrorResponse; |
| 5 | +import jakarta.validation.ConstraintViolationException; |
| 6 | +import java.io.IOException; |
| 7 | +import java.util.List; |
| 8 | +import lombok.extern.slf4j.Slf4j; |
| 9 | +import org.springframework.http.ResponseEntity; |
| 10 | +import org.springframework.http.converter.HttpMessageNotReadableException; |
| 11 | +import org.springframework.validation.BindException; |
| 12 | +import org.springframework.validation.FieldError; |
| 13 | +import org.springframework.validation.ObjectError; |
| 14 | +import org.springframework.web.HttpMediaTypeNotSupportedException; |
| 15 | +import org.springframework.web.HttpRequestMethodNotSupportedException; |
| 16 | +import org.springframework.web.bind.MethodArgumentNotValidException; |
| 17 | +import org.springframework.web.bind.MissingServletRequestParameterException; |
| 18 | +import org.springframework.web.bind.annotation.ExceptionHandler; |
| 19 | +import org.springframework.web.bind.annotation.RestControllerAdvice; |
| 20 | +import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; |
| 21 | +import org.springframework.web.servlet.resource.NoResourceFoundException; |
| 22 | + |
| 23 | +@Slf4j |
| 24 | +@RestControllerAdvice |
| 25 | +public class GlobalExceptionHandler { |
| 26 | + |
| 27 | + @ExceptionHandler(BootSignalException.class) |
| 28 | + public ResponseEntity<ApiResponse<Void>> handleBootSignalException(BootSignalException exception) { |
| 29 | + ErrorCode errorCode = exception.errorCode(); |
| 30 | + return toResponse(errorCode, exception.getMessage()); |
| 31 | + } |
| 32 | + |
| 33 | + @ExceptionHandler(MethodArgumentNotValidException.class) |
| 34 | + public ResponseEntity<ApiResponse<Void>> handleMethodArgumentNotValidException( |
| 35 | + MethodArgumentNotValidException exception |
| 36 | + ) { |
| 37 | + return toResponse(ErrorCode.VALIDATION_ERROR, ErrorCode.VALIDATION_ERROR.message(), fieldErrors(exception)); |
| 38 | + } |
| 39 | + |
| 40 | + @ExceptionHandler(BindException.class) |
| 41 | + public ResponseEntity<ApiResponse<Void>> handleBindException(BindException exception) { |
| 42 | + return toResponse(ErrorCode.VALIDATION_ERROR, ErrorCode.VALIDATION_ERROR.message(), fieldErrors(exception)); |
| 43 | + } |
| 44 | + |
| 45 | + @ExceptionHandler(ConstraintViolationException.class) |
| 46 | + public ResponseEntity<ApiResponse<Void>> handleConstraintViolationException( |
| 47 | + ConstraintViolationException exception |
| 48 | + ) { |
| 49 | + // 메서드 파라미터 검증 실패를 필드 단위 오류로 변환한다. |
| 50 | + List<ErrorResponse.FieldError> fieldErrors = exception.getConstraintViolations().stream() |
| 51 | + .map(violation -> new ErrorResponse.FieldError( |
| 52 | + violation.getPropertyPath().toString(), |
| 53 | + violation.getMessage() |
| 54 | + )) |
| 55 | + .toList(); |
| 56 | + return toResponse(ErrorCode.VALIDATION_ERROR, ErrorCode.VALIDATION_ERROR.message(), fieldErrors); |
| 57 | + } |
| 58 | + |
| 59 | + @ExceptionHandler({ |
| 60 | + HttpMessageNotReadableException.class, |
| 61 | + MissingServletRequestParameterException.class, |
| 62 | + MethodArgumentTypeMismatchException.class, |
| 63 | + IllegalArgumentException.class |
| 64 | + }) |
| 65 | + public ResponseEntity<ApiResponse<Void>> handleBadRequestException(Exception exception) { |
| 66 | + return toResponse(ErrorCode.BAD_REQUEST, resolveMessage(exception, ErrorCode.BAD_REQUEST.message())); |
| 67 | + } |
| 68 | + |
| 69 | + @ExceptionHandler(NoResourceFoundException.class) |
| 70 | + public ResponseEntity<ApiResponse<Void>> handleNoResourceFoundException(NoResourceFoundException exception) { |
| 71 | + return toResponse(ErrorCode.NOT_FOUND, ErrorCode.NOT_FOUND.message()); |
| 72 | + } |
| 73 | + |
| 74 | + @ExceptionHandler(HttpRequestMethodNotSupportedException.class) |
| 75 | + public ResponseEntity<ApiResponse<Void>> handleMethodNotSupportedException( |
| 76 | + HttpRequestMethodNotSupportedException exception |
| 77 | + ) { |
| 78 | + return toResponse(ErrorCode.METHOD_NOT_ALLOWED, ErrorCode.METHOD_NOT_ALLOWED.message()); |
| 79 | + } |
| 80 | + |
| 81 | + @ExceptionHandler(HttpMediaTypeNotSupportedException.class) |
| 82 | + public ResponseEntity<ApiResponse<Void>> handleMediaTypeNotSupportedException( |
| 83 | + HttpMediaTypeNotSupportedException exception |
| 84 | + ) { |
| 85 | + return toResponse(ErrorCode.UNSUPPORTED_MEDIA_TYPE, ErrorCode.UNSUPPORTED_MEDIA_TYPE.message()); |
| 86 | + } |
| 87 | + |
| 88 | + @ExceptionHandler(IOException.class) |
| 89 | + public ResponseEntity<ApiResponse<Void>> handleIOException(IOException exception) { |
| 90 | + // 내부 상세 경로나 시스템 메시지가 응답에 노출되지 않도록 고정 메시지를 사용한다. |
| 91 | + log.warn("I/O exception occurred", exception); |
| 92 | + return toResponse(ErrorCode.INTERNAL_SERVER_ERROR, "입출력 처리 중 오류가 발생했습니다."); |
| 93 | + } |
| 94 | + |
| 95 | + @ExceptionHandler(Exception.class) |
| 96 | + public ResponseEntity<ApiResponse<Void>> handleException(Exception exception) { |
| 97 | + log.error("Unexpected exception occurred", exception); |
| 98 | + return toResponse(ErrorCode.INTERNAL_SERVER_ERROR, ErrorCode.INTERNAL_SERVER_ERROR.message()); |
| 99 | + } |
| 100 | + |
| 101 | + private ResponseEntity<ApiResponse<Void>> toResponse(ErrorCode errorCode, String message) { |
| 102 | + return toResponse(errorCode, message, List.of()); |
| 103 | + } |
| 104 | + |
| 105 | + private ResponseEntity<ApiResponse<Void>> toResponse( |
| 106 | + ErrorCode errorCode, |
| 107 | + String message, |
| 108 | + List<ErrorResponse.FieldError> fieldErrors |
| 109 | + ) { |
| 110 | + ErrorResponse errorResponse = ErrorResponse.of(errorCode, message, fieldErrors); |
| 111 | + return ResponseEntity.status(errorCode.status()).body(ApiResponse.failure(errorResponse)); |
| 112 | + } |
| 113 | + |
| 114 | + private List<ErrorResponse.FieldError> fieldErrors(BindException exception) { |
| 115 | + return exception.getBindingResult().getFieldErrors().stream() |
| 116 | + .map(this::toFieldError) |
| 117 | + .toList(); |
| 118 | + } |
| 119 | + |
| 120 | + private ErrorResponse.FieldError toFieldError(FieldError fieldError) { |
| 121 | + return new ErrorResponse.FieldError(fieldError.getField(), resolveMessage(fieldError)); |
| 122 | + } |
| 123 | + |
| 124 | + private String resolveMessage(ObjectError objectError) { |
| 125 | + return resolveMessage(objectError.getDefaultMessage(), "유효하지 않은 값입니다."); |
| 126 | + } |
| 127 | + |
| 128 | + private String resolveMessage(Exception exception, String defaultMessage) { |
| 129 | + return resolveMessage(exception.getMessage(), defaultMessage); |
| 130 | + } |
| 131 | + |
| 132 | + private String resolveMessage(String message, String defaultMessage) { |
| 133 | + return message == null || message.isBlank() ? defaultMessage : message; |
| 134 | + } |
| 135 | +} |
0 commit comments