diff --git a/src/main/java/today/meevote/controllerAdvice/ControllerAdvice.java b/src/main/java/today/meevote/controllerAdvice/GlobalExceptionHandler.java similarity index 50% rename from src/main/java/today/meevote/controllerAdvice/ControllerAdvice.java rename to src/main/java/today/meevote/controllerAdvice/GlobalExceptionHandler.java index 7fc2a39..a1cb845 100644 --- a/src/main/java/today/meevote/controllerAdvice/ControllerAdvice.java +++ b/src/main/java/today/meevote/controllerAdvice/GlobalExceptionHandler.java @@ -1,14 +1,17 @@ package today.meevote.controllerAdvice; -import org.springframework.web.bind.annotation.ExceptionHandler; - import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; import today.meevote.exception.view.UnauthenticatedException; import today.meevote.exception.view.NotFoundException; +import today.meevote.response.BaseResponse; +import today.meevote.response.FailureInfo; -@org.springframework.web.bind.annotation.ControllerAdvice @Slf4j -public class ControllerAdvice { +@ControllerAdvice +public class GlobalExceptionHandler { @ExceptionHandler(UnauthenticatedException.class) public String unauthenticatedException(UnauthenticatedException e){ @@ -21,5 +24,15 @@ public String notFoundException(NotFoundException e) { log.error("notFoundException", e); return "redirect:/notfound"; } - + + + @ExceptionHandler(MethodArgumentNotValidException.class) + protected BaseResponse methodArgumentNotValidException(MethodArgumentNotValidException e){ + log.error("invalidInputException", e); + return new BaseResponse( + false, + FailureInfo.INVALID_INPUT.getCode(), + e.getFieldError().getDefaultMessage() + ); + } } diff --git a/src/main/java/today/meevote/controllerAdvice/RestControllerAdvice.java b/src/main/java/today/meevote/controllerAdvice/RestGlobalExceptionHandler.java similarity index 92% rename from src/main/java/today/meevote/controllerAdvice/RestControllerAdvice.java rename to src/main/java/today/meevote/controllerAdvice/RestGlobalExceptionHandler.java index 913a484..d9f51f2 100644 --- a/src/main/java/today/meevote/controllerAdvice/RestControllerAdvice.java +++ b/src/main/java/today/meevote/controllerAdvice/RestGlobalExceptionHandler.java @@ -5,13 +5,13 @@ import jakarta.validation.ConstraintViolationException; import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.RestControllerAdvice; import today.meevote.exception.rest.RestException; import today.meevote.response.BaseResponse; import today.meevote.response.FailureInfo; - -@org.springframework.web.bind.annotation.RestControllerAdvice @Slf4j -public class RestControllerAdvice { +@RestControllerAdvice +public class RestGlobalExceptionHandler { @ExceptionHandler(Exception.class) protected BaseResponse internalSeverException(Exception e){ @@ -38,7 +38,7 @@ protected BaseResponse constraintViolationException(ConstraintViolationException e.getConstraintViolations().stream().toList().get(0).getMessage() ); } - + @ExceptionHandler(RestException.class) protected BaseResponse restException(RestException e){ log.error("restException", e);