|
4 | 4 | import com.arom.with_travel.global.exception.error.ErrorCode; |
5 | 5 | import com.arom.with_travel.global.exception.error.ErrorDisplayType; |
6 | 6 | import com.arom.with_travel.global.exception.response.ErrorResponse; |
| 7 | +import jakarta.validation.ConstraintViolationException; |
7 | 8 | import lombok.extern.slf4j.Slf4j; |
| 9 | +import org.springframework.beans.TypeMismatchException; |
| 10 | +import org.springframework.context.MessageSourceResolvable; |
| 11 | +import org.springframework.dao.DataIntegrityViolationException; |
| 12 | +import org.springframework.http.ResponseEntity; |
| 13 | +import org.springframework.http.converter.HttpMessageNotReadableException; |
| 14 | +import org.springframework.web.HttpRequestMethodNotSupportedException; |
8 | 15 | import org.springframework.web.bind.MethodArgumentNotValidException; |
| 16 | +import org.springframework.web.bind.MissingServletRequestParameterException; |
9 | 17 | import org.springframework.web.bind.annotation.ExceptionHandler; |
10 | 18 | import org.springframework.web.bind.annotation.RestController; |
11 | 19 | import org.springframework.web.bind.annotation.RestControllerAdvice; |
| 20 | +import org.springframework.web.method.annotation.HandlerMethodValidationException; |
12 | 21 |
|
13 | 22 | @Slf4j |
14 | 23 | @RestControllerAdvice(annotations = {RestController.class}) |
15 | 24 | public class GlobalExceptionHandler { |
16 | 25 |
|
17 | | - /** |
18 | | - * 클라이언트 에러 |
19 | | - * 직접 생성한 예외에 대한 처리 |
20 | | - */ |
| 26 | + // HTTP Method 불일치 에러 |
| 27 | + @ExceptionHandler(HttpRequestMethodNotSupportedException.class) |
| 28 | + public ErrorResponse handleHttpRequestMethodNotSupportedException( |
| 29 | + HttpRequestMethodNotSupportedException e) { |
| 30 | + return ErrorResponse.generateFrom(ErrorCode.METHOD_NOT_ALLOWED); |
| 31 | + } |
| 32 | + |
| 33 | + // @RequestBody JSON 파싱 에러 |
| 34 | + @ExceptionHandler(HttpMessageNotReadableException.class) |
| 35 | + public ErrorResponse handleJsonParseException(HttpMessageNotReadableException e) { |
| 36 | + return ErrorResponse.generateFrom(ErrorCode.INVALID_JSON_FORMAT); |
| 37 | + } |
| 38 | + |
| 39 | + // 필수 @RequestParam 누락 |
| 40 | + @ExceptionHandler(MissingServletRequestParameterException.class) |
| 41 | + public ErrorResponse handleMissingParam(MissingServletRequestParameterException e) { |
| 42 | + return ErrorResponse.generateFrom(ErrorCode.MISSING_PARAMETER); |
| 43 | + } |
| 44 | + |
| 45 | + // @RequestParam 타입 불일치 |
| 46 | + @ExceptionHandler(TypeMismatchException.class) |
| 47 | + public ErrorResponse handleTypeMismatchException(TypeMismatchException e) { |
| 48 | + return ErrorResponse.generateFrom(ErrorCode.INVALID_PARAMETER_TYPE); |
| 49 | + } |
| 50 | + |
| 51 | + // 데이터 무결성 위반 |
| 52 | + @ExceptionHandler(DataIntegrityViolationException.class) |
| 53 | + public ErrorResponse handleDataIntegrityViolationException(DataIntegrityViolationException ex) { |
| 54 | + return ErrorResponse.generateFrom(ErrorCode.ERR_DATA_INTEGRITY_VIOLATION); |
| 55 | + } |
| 56 | + |
21 | 57 | @ExceptionHandler(BaseException.class) |
22 | 58 | public ErrorResponse onThrowException(BaseException baseException) { |
23 | | - return ErrorResponse.generateErrorResponse(baseException); |
| 59 | + return ErrorResponse.generateFrom(baseException); |
24 | 60 | } |
25 | 61 |
|
26 | | - // TODO : 바인딩 에러 관련 ErrorCode 작성 |
27 | 62 | @ExceptionHandler(MethodArgumentNotValidException.class) |
28 | 63 | public ErrorResponse onThrowException(MethodArgumentNotValidException exception){ |
29 | | - return ErrorResponse.builder() |
30 | | - .code(ErrorCode.TMP_ERROR.getCode()) |
31 | | - .message(exception.getBindingResult().getFieldError().getDefaultMessage()) |
32 | | - .displayType(ErrorDisplayType.POPUP) |
33 | | - .build(); |
| 64 | + String message = exception.getBindingResult().getFieldError().getDefaultMessage(); |
| 65 | + return ErrorResponse.generateWithCustomMessage(ErrorCode.REQ_BODY_ERROR, message); |
| 66 | + } |
| 67 | + |
| 68 | + @ExceptionHandler(HandlerMethodValidationException.class) |
| 69 | + public ErrorResponse onThrowException(HandlerMethodValidationException exception){ |
| 70 | + String message = exception |
| 71 | + .getAllErrors() |
| 72 | + .stream() |
| 73 | + .map(MessageSourceResolvable::getDefaultMessage) |
| 74 | + .findFirst() |
| 75 | + .orElse("Validation Failed"); |
| 76 | + return ErrorResponse.generateWithCustomMessage(ErrorCode.REQ_PARAMS_ERROR, message); |
| 77 | + } |
| 78 | + |
| 79 | + @ExceptionHandler(Exception.class) |
| 80 | + protected ErrorResponse handleException(Exception e) { |
| 81 | + return ErrorResponse.generateFrom(ErrorCode.INTERNAL_SERVER_ERROR); |
34 | 82 | } |
35 | 83 | } |
0 commit comments