|
| 1 | +package com.example.ssccwebbe.global.apipayload.handler; |
| 2 | + |
| 3 | +import org.springframework.http.HttpHeaders; |
| 4 | +import org.springframework.http.HttpStatusCode; |
| 5 | +import org.springframework.http.ResponseEntity; |
| 6 | +import org.springframework.http.converter.HttpMessageNotReadableException; |
| 7 | +import org.springframework.validation.ObjectError; |
| 8 | +import org.springframework.web.HttpRequestMethodNotSupportedException; |
| 9 | +import org.springframework.web.bind.MethodArgumentNotValidException; |
| 10 | +import org.springframework.web.bind.annotation.ExceptionHandler; |
| 11 | +import org.springframework.web.bind.annotation.RestController; |
| 12 | +import org.springframework.web.bind.annotation.RestControllerAdvice; |
| 13 | +import org.springframework.web.context.request.WebRequest; |
| 14 | +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; |
| 15 | + |
| 16 | +import com.example.ssccwebbe.global.apipayload.ApiResponse; |
| 17 | +import com.example.ssccwebbe.global.apipayload.code.error.CommonErrorCode; |
| 18 | +import com.example.ssccwebbe.global.apipayload.code.error.ErrorCode; |
| 19 | +import com.example.ssccwebbe.global.apipayload.exception.GeneralException; |
| 20 | +import com.fasterxml.jackson.databind.exc.InvalidFormatException; |
| 21 | + |
| 22 | +import lombok.extern.slf4j.Slf4j; |
| 23 | + |
| 24 | +@Slf4j |
| 25 | +@RestControllerAdvice(annotations = {RestController.class}) |
| 26 | +public class GlobalExceptionHandler extends ResponseEntityExceptionHandler { |
| 27 | + |
| 28 | + @ExceptionHandler(GeneralException.class) |
| 29 | + public ResponseEntity<Object> handleGeneralException(GeneralException ex) { |
| 30 | + ErrorCode errorCode = ex.getErrorCode(); |
| 31 | + return handleExceptionInternal(errorCode); |
| 32 | + } |
| 33 | + |
| 34 | + @ExceptionHandler(IllegalArgumentException.class) |
| 35 | + public ResponseEntity<Object> handleIllegalArgument(IllegalArgumentException ex) { |
| 36 | + log.warn("IllegalArgumentException: {}", ex.getMessage()); |
| 37 | + ErrorCode errorCode = CommonErrorCode.INVALID_PARAMETER; |
| 38 | + return handleExceptionInternal(errorCode, ex.getMessage()); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + protected ResponseEntity<Object> handleHttpRequestMethodNotSupported( |
| 43 | + HttpRequestMethodNotSupportedException ex, |
| 44 | + HttpHeaders headers, |
| 45 | + HttpStatusCode status, |
| 46 | + WebRequest request) { |
| 47 | + log.warn("HttpRequestMethodNotSupportedException: {}", ex.getMessage()); |
| 48 | + ErrorCode errorCode = CommonErrorCode.METHOD_NOT_ALLOWED; |
| 49 | + return handleExceptionInternal(errorCode); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + protected ResponseEntity<Object> handleMethodArgumentNotValid( |
| 54 | + MethodArgumentNotValidException ex, |
| 55 | + HttpHeaders headers, |
| 56 | + HttpStatusCode status, |
| 57 | + WebRequest request) { |
| 58 | + log.warn("MethodArgumentNotValidException"); |
| 59 | + ErrorCode errorCode = CommonErrorCode.INVALID_PARAMETER; |
| 60 | + return handleExceptionInternal(errorCode, getDefaultMessage(ex)); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + protected ResponseEntity<Object> handleHttpMessageNotReadable( |
| 65 | + HttpMessageNotReadableException ex, |
| 66 | + HttpHeaders headers, |
| 67 | + HttpStatusCode status, |
| 68 | + WebRequest request) { |
| 69 | + // Jackson이 DTO로 변환하다 실패(예: enum/숫자 타입 변환 실패)한 경우 상세 메시지 제공 |
| 70 | + Throwable cause = ex.getCause(); |
| 71 | + if (cause instanceof InvalidFormatException ife && !ife.getPath().isEmpty()) { |
| 72 | + String fieldName = ife.getPath().get(0).getFieldName(); |
| 73 | + String value = String.valueOf(ife.getValue()); |
| 74 | + String targetType = |
| 75 | + ife.getTargetType() != null ? ife.getTargetType().getSimpleName() : "Unknown"; |
| 76 | + |
| 77 | + String message = |
| 78 | + String.format( |
| 79 | + "'%s'는 %s 필드에 유효하지 않은 값입니다. (%s 타입)", value, fieldName, targetType); |
| 80 | + ErrorCode errorCode = CommonErrorCode.INVALID_BODY; |
| 81 | + return handleExceptionInternal(errorCode, message); |
| 82 | + } |
| 83 | + |
| 84 | + ErrorCode errorCode = CommonErrorCode.INVALID_BODY; |
| 85 | + return handleExceptionInternal(errorCode, CommonErrorCode.INVALID_BODY.getMessage()); |
| 86 | + } |
| 87 | + |
| 88 | + @ExceptionHandler(Exception.class) |
| 89 | + public ResponseEntity<Object> handleAll(Exception ex) { |
| 90 | + log.error("Unhandled Exception", ex); |
| 91 | + ErrorCode errorCode = CommonErrorCode.INTERNAL_SERVER_ERROR; |
| 92 | + return handleExceptionInternal(errorCode); |
| 93 | + } |
| 94 | + |
| 95 | + private static String getDefaultMessage(MethodArgumentNotValidException ex) { |
| 96 | + StringBuilder message = new StringBuilder(); |
| 97 | + for (ObjectError error : ex.getBindingResult().getAllErrors()) { |
| 98 | + message.append(error.getDefaultMessage()).append(" "); |
| 99 | + } |
| 100 | + return message.toString().trim(); |
| 101 | + } |
| 102 | + |
| 103 | + private ResponseEntity<Object> handleExceptionInternal(final ErrorCode errorCode) { |
| 104 | + return ResponseEntity.status(errorCode.getHttpStatus()).body(ApiResponse.fail(errorCode)); |
| 105 | + } |
| 106 | + |
| 107 | + private ResponseEntity<Object> handleExceptionInternal( |
| 108 | + final ErrorCode errorCode, final String message) { |
| 109 | + return ResponseEntity.status(errorCode.getHttpStatus()) |
| 110 | + .body(ApiResponse.fail(errorCode, message)); |
| 111 | + } |
| 112 | +} |
0 commit comments