|
| 1 | +package com.systemdesign.slidingwindowlog.common.exception; |
| 2 | + |
| 3 | +import com.systemdesign.slidingwindowlog.common.dto.ExceptionResponse; |
| 4 | +import org.springframework.http.ResponseEntity; |
| 5 | +import org.springframework.web.HttpRequestMethodNotSupportedException; |
| 6 | +import org.springframework.web.bind.MethodArgumentNotValidException; |
| 7 | +import org.springframework.web.bind.annotation.ExceptionHandler; |
| 8 | +import org.springframework.web.bind.annotation.RestControllerAdvice; |
| 9 | +import org.springframework.web.servlet.NoHandlerFoundException; |
| 10 | + |
| 11 | +import static com.systemdesign.slidingwindowlog.common.exception.CommonExceptionCode.*; |
| 12 | + |
| 13 | +@RestControllerAdvice |
| 14 | +public class GlobalExceptionHandler { |
| 15 | + |
| 16 | + @ExceptionHandler(NoHandlerFoundException.class) |
| 17 | + public ResponseEntity<ExceptionResponse> handleNoHandlerFoundException(NoHandlerFoundException e) { |
| 18 | + return ResponseEntity.status(COMMON_NOT_FOUND.getStatus()) |
| 19 | + .body(ExceptionResponse.from(COMMON_NOT_FOUND.getCode())); |
| 20 | + } |
| 21 | + |
| 22 | + @ExceptionHandler(MethodArgumentNotValidException.class) |
| 23 | + public ResponseEntity<ExceptionResponse> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) { |
| 24 | + return ResponseEntity.status(COMMON_BAD_REQUEST.getStatus()) |
| 25 | + .body(ExceptionResponse.from(COMMON_BAD_REQUEST.getCode())); |
| 26 | + } |
| 27 | + |
| 28 | + @ExceptionHandler(HttpRequestMethodNotSupportedException.class) |
| 29 | + public ResponseEntity<ExceptionResponse> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) { |
| 30 | + return ResponseEntity.status(COMMON_METHOD_NOT_ALLOWED.getStatus()) |
| 31 | + .body(ExceptionResponse.from(COMMON_METHOD_NOT_ALLOWED.getCode())); |
| 32 | + } |
| 33 | + |
| 34 | + @ExceptionHandler(Exception.class) |
| 35 | + public ResponseEntity<ExceptionResponse> handleException(Exception e) { |
| 36 | + return ResponseEntity.status(COMMON_INTERNAL_SERVER_ERROR.getStatus()) |
| 37 | + .body(ExceptionResponse.from(COMMON_INTERNAL_SERVER_ERROR.getCode())); |
| 38 | + } |
| 39 | + |
| 40 | + @ExceptionHandler(BusinessException.class) |
| 41 | + public ResponseEntity<ExceptionResponse> handleBusinessException(BusinessException e) { |
| 42 | + ExceptionCode exceptionCode = e.getExceptionCode(); |
| 43 | + return ResponseEntity.status(exceptionCode.getStatus()) |
| 44 | + .body(ExceptionResponse.from(exceptionCode.getCode())); |
| 45 | + } |
| 46 | +} |
0 commit comments