From 2e30237d9c3ed50dfa524fdce7d88eaf9daaecc1 Mon Sep 17 00:00:00 2001 From: SangWoon123 Date: Wed, 5 Jun 2024 00:45:16 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20Exception=20=ED=95=B8=EB=93=A4=EB=9F=AC?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../error/GlobalExceptionController.java | 50 ++++++++++++++++++- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/tukorea/planding/global/error/GlobalExceptionController.java b/src/main/java/com/tukorea/planding/global/error/GlobalExceptionController.java index 12dbacc..9aea425 100644 --- a/src/main/java/com/tukorea/planding/global/error/GlobalExceptionController.java +++ b/src/main/java/com/tukorea/planding/global/error/GlobalExceptionController.java @@ -1,17 +1,63 @@ package com.tukorea.planding.global.error; +import com.fasterxml.jackson.databind.exc.InvalidFormatException; import com.tukorea.planding.common.CommonResponse; import com.tukorea.planding.common.CommonUtils; -import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.servlet.NoHandlerFoundException; + +import java.util.HashMap; +import java.util.Map; @RestControllerAdvice public class GlobalExceptionController { - @ExceptionHandler(value = { BusinessException.class }) + + /* + 잘못된 URL 예외 + */ + @ExceptionHandler(NoHandlerFoundException.class) + public CommonResponse handleNoHandlerFoundException() { + ErrorResponse errorResponse = new ErrorResponse(ErrorCode.INVALID_URL); + return CommonUtils.fail(errorResponse); + } + + /* + NULL 값 + */ + @ExceptionHandler(NullPointerException.class) + public CommonResponse handleNoHandlerNullPointException() { + ErrorResponse errorResponse = new ErrorResponse(ErrorCode.INVALID_URL); + return CommonUtils.fail(errorResponse); + } + + /* + @Valid 핸들러 + */ + @ExceptionHandler(MethodArgumentNotValidException.class) + public ResponseEntity> handleValidationExceptions(MethodArgumentNotValidException ex) { + Map errors = new HashMap<>(); + ex.getBindingResult().getFieldErrors().forEach(error -> + errors.put(error.getField(), error.getDefaultMessage())); + return new ResponseEntity<>(errors, HttpStatus.BAD_REQUEST); + } + + /* + ErrorCode 핸들러 + */ + @ExceptionHandler(value = {BusinessException.class}) protected CommonResponse handleBusinessException(BusinessException ex) { final ErrorCode errorCode = ex.getErrorCode(); final ErrorResponse errorResponse = ErrorResponse.of(errorCode); return CommonUtils.fail(errorResponse); } + + @ExceptionHandler(InvalidFormatException.class) + public ResponseEntity handleInvalidFormatException(InvalidFormatException ex) { + ErrorResponse errorResponse = new ErrorResponse(ErrorCode.INVALID_REQUEST); + return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST); + } }