diff --git a/cakey-api/src/main/java/com/cakey/cake/exception/CakeApiBaseException.java b/cakey-api/src/main/java/com/cakey/cake/exception/CakeApiBaseException.java index f5aa523..d14739e 100644 --- a/cakey-api/src/main/java/com/cakey/cake/exception/CakeApiBaseException.java +++ b/cakey-api/src/main/java/com/cakey/cake/exception/CakeApiBaseException.java @@ -3,10 +3,12 @@ import com.cakey.rescode.ErrorCode; import com.cakey.exception.CakeBaseException; import lombok.AccessLevel; +import lombok.Getter; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; @RequiredArgsConstructor(access = AccessLevel.PROTECTED) +@Getter public abstract class CakeApiBaseException extends CakeBaseException { ///abstract 이유 : 추상 클래스로 상속받는 자식 클래스가 구체적인 구현을 제공하도록 강제 private final ErrorCode errorCode; diff --git a/cakey-api/src/main/java/com/cakey/common/response/ApiResponseUtil.java b/cakey-api/src/main/java/com/cakey/common/response/ApiResponseUtil.java index 43d4dea..e6ba561 100644 --- a/cakey-api/src/main/java/com/cakey/common/response/ApiResponseUtil.java +++ b/cakey-api/src/main/java/com/cakey/common/response/ApiResponseUtil.java @@ -1,6 +1,7 @@ package com.cakey.common.response; import com.cakey.rescode.ErrorBaseCode; +import com.cakey.rescode.ErrorCode; import com.cakey.rescode.SuccessCode; import org.springframework.http.ResponseEntity; @@ -15,13 +16,13 @@ public static ResponseEntity> success(final SuccessCode succ .body(BaseResponse.of(successCode, data)); } - public static ResponseEntity> failure(final ErrorBaseCode errorBaseCode) { + public static ResponseEntity> failure(final ErrorCode errorBaseCode) { return ResponseEntity.status(errorBaseCode.getHttpStatus()) .body(BaseResponse.of(errorBaseCode)); } //따로 error message 넣어줄 때, 사용 - public static ResponseEntity> failure(final ErrorBaseCode errorBaseCode, final String message) { + public static ResponseEntity> failure(final ErrorCode errorBaseCode, final String message) { return ResponseEntity .status(errorBaseCode.getHttpStatus()) .body(BaseResponse.of(errorBaseCode.getCode(), message)); diff --git a/cakey-api/src/main/java/com/cakey/exception/handler/GlobalExceptionHandler.java b/cakey-api/src/main/java/com/cakey/exception/handler/GlobalExceptionHandler.java index e7998fc..dd9dafc 100644 --- a/cakey-api/src/main/java/com/cakey/exception/handler/GlobalExceptionHandler.java +++ b/cakey-api/src/main/java/com/cakey/exception/handler/GlobalExceptionHandler.java @@ -1,8 +1,11 @@ package com.cakey.exception.handler; +import com.cakey.cake.exception.CakeApiBaseException; import com.cakey.rescode.ErrorBaseCode; import com.cakey.common.response.ApiResponseUtil; import com.cakey.common.response.BaseResponse; +import com.cakey.store.exception.StoreApiBaseException; +import com.cakey.user.exception.UserApiBaseException; import com.fasterxml.jackson.databind.JsonMappingException; import jakarta.persistence.EntityNotFoundException; import lombok.val; @@ -28,19 +31,26 @@ @RestControllerAdvice public class GlobalExceptionHandler { + @ExceptionHandler(CakeApiBaseException.class) + public ResponseEntity> handleCakeApiBaseException(final CakeApiBaseException e) { + return ApiResponseUtil.failure(e.getErrorCode()); + } -// /** -// * CakeyException -// */ -// @ExceptionHandler(CakeyException.class) -// protected ResponseEntity> handleAgodaException(final AgodaException e) { -// return ApiResponseUtil.failure(e.getFailMessage()); -// } + @ExceptionHandler(StoreApiBaseException.class) + public ResponseEntity> handleStoreApiBaseException(final StoreApiBaseException e) { + return ApiResponseUtil.failure(e.getErrorCode()); + } - /** - * 400 - MethodArgumentNotValidException - * 발생 이유 : @Valid 검증 실패 (@Request Body) - */ + @ExceptionHandler(UserApiBaseException.class) + public ResponseEntity> handleUserApiBaseException(final UserApiBaseException e) { + return ApiResponseUtil.failure(e.getErrorCode()); + } + + + /** + * 400 - MethodArgumentNotValidException + * 발생 이유 : @Valid 검증 실패 (@Request Body) + */ @ExceptionHandler(MethodArgumentNotValidException.class) public ResponseEntity> handleMethodArgumentNotValidException(final MethodArgumentNotValidException e) { // 모든 검증 실패 메시지를 추출하고 줄바꿈으로 연결 diff --git a/cakey-api/src/main/java/com/cakey/store/exception/StoreApiBaseException.java b/cakey-api/src/main/java/com/cakey/store/exception/StoreApiBaseException.java index e981ab8..23b240f 100644 --- a/cakey-api/src/main/java/com/cakey/store/exception/StoreApiBaseException.java +++ b/cakey-api/src/main/java/com/cakey/store/exception/StoreApiBaseException.java @@ -3,10 +3,12 @@ import com.cakey.rescode.ErrorCode; import com.cakey.exception.CakeBaseException; import lombok.AccessLevel; +import lombok.Getter; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; @RequiredArgsConstructor(access = AccessLevel.PROTECTED) +@Getter public abstract class StoreApiBaseException extends CakeBaseException { private final ErrorCode errorCode; diff --git a/cakey-api/src/main/java/com/cakey/user/exception/UserApiBaseException.java b/cakey-api/src/main/java/com/cakey/user/exception/UserApiBaseException.java index 97b1742..51fa6e1 100644 --- a/cakey-api/src/main/java/com/cakey/user/exception/UserApiBaseException.java +++ b/cakey-api/src/main/java/com/cakey/user/exception/UserApiBaseException.java @@ -3,10 +3,12 @@ import com.cakey.exception.CakeBaseException; import com.cakey.rescode.ErrorCode; import lombok.AccessLevel; +import lombok.Getter; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; @RequiredArgsConstructor(access = AccessLevel.PROTECTED) +@Getter public abstract class UserApiBaseException extends CakeBaseException { private final ErrorCode errorCode;