From 1e59bab2e841355f65050cee2213dd758e1950e9 Mon Sep 17 00:00:00 2001 From: kgy1008 Date: Tue, 16 Jul 2024 15:35:47 +0900 Subject: [PATCH] [feat] logic for creat fail response --- server-yml | 2 +- .../hankkiserver/api/advice/GlobalExceptionHandler.java | 5 ++--- .../org/hankki/hankkiserver/api/dto/HankkiResponse.java | 4 ++++ .../hankkiserver/api/store/service/StoreQueryService.java | 2 +- .../hankkiserver/common/exception/ConflictException.java | 7 +++++++ 5 files changed, 15 insertions(+), 5 deletions(-) diff --git a/server-yml b/server-yml index 3e46404d..7271745f 160000 --- a/server-yml +++ b/server-yml @@ -1 +1 @@ -Subproject commit 3e46404da87abd369a875ff5e2b95b2131b18647 +Subproject commit 7271745fce0c694f42ab8e5d7fe3655b3fa18e4a diff --git a/src/main/java/org/hankki/hankkiserver/api/advice/GlobalExceptionHandler.java b/src/main/java/org/hankki/hankkiserver/api/advice/GlobalExceptionHandler.java index 44e6cad8..9b41dfff 100644 --- a/src/main/java/org/hankki/hankkiserver/api/advice/GlobalExceptionHandler.java +++ b/src/main/java/org/hankki/hankkiserver/api/advice/GlobalExceptionHandler.java @@ -7,7 +7,6 @@ import org.hankki.hankkiserver.common.exception.ConflictException; import org.hankki.hankkiserver.common.exception.NotFoundException; import org.hankki.hankkiserver.common.exception.UnauthorizedException; - import org.springframework.web.bind.MissingServletRequestParameterException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; @@ -35,9 +34,9 @@ public HankkiResponse handleEntityNotFoundException(NotFoundException e) { } @ExceptionHandler(ConflictException.class) - public HankkiResponse handleConflictException(ConflictException e) { + public HankkiResponse handleConflictException(ConflictException e) { log.error("handleConflictException() in GlobalExceptionHandler throw ConflictException : {}", e.getMessage()); - return HankkiResponse.fail(e.getErrorCode()); + return HankkiResponse.fail(e.getErrorCode(), e.getData()); } @ExceptionHandler(MissingServletRequestParameterException.class) diff --git a/src/main/java/org/hankki/hankkiserver/api/dto/HankkiResponse.java b/src/main/java/org/hankki/hankkiserver/api/dto/HankkiResponse.java index 4e2d6b00..46e548e9 100644 --- a/src/main/java/org/hankki/hankkiserver/api/dto/HankkiResponse.java +++ b/src/main/java/org/hankki/hankkiserver/api/dto/HankkiResponse.java @@ -27,4 +27,8 @@ public static HankkiResponse success(SuccessCode success, T data) { public static HankkiResponse fail(ErrorCode error) { return new HankkiResponse<>(error.getHttpStatus().value(), error.getMessage()); } + + public static HankkiResponse fail(ErrorCode error, T data) { + return new HankkiResponse<>(error.getHttpStatus().value(), error.getMessage(), data); + } } diff --git a/src/main/java/org/hankki/hankkiserver/api/store/service/StoreQueryService.java b/src/main/java/org/hankki/hankkiserver/api/store/service/StoreQueryService.java index 33a1b6bd..74f4164e 100644 --- a/src/main/java/org/hankki/hankkiserver/api/store/service/StoreQueryService.java +++ b/src/main/java/org/hankki/hankkiserver/api/store/service/StoreQueryService.java @@ -89,7 +89,7 @@ public void validateDuplicatedStore(final StoreValidationCommand command) { private void findUniversityStore(final Long universityId, final Store store) { universityStoreFinder.findByUniversityIdAndStore(universityId, store) .ifPresent(universityStore -> { - throw new ConflictException(StoreErrorCode.STORE_ALREADY_REGISTERED); + throw new ConflictException(StoreErrorCode.STORE_ALREADY_REGISTERED, new StoreDuplicateValidationResponse(store.getId())); }); } } diff --git a/src/main/java/org/hankki/hankkiserver/common/exception/ConflictException.java b/src/main/java/org/hankki/hankkiserver/common/exception/ConflictException.java index aa2d9182..b55fa18e 100644 --- a/src/main/java/org/hankki/hankkiserver/common/exception/ConflictException.java +++ b/src/main/java/org/hankki/hankkiserver/common/exception/ConflictException.java @@ -6,9 +6,16 @@ @Getter public class ConflictException extends RuntimeException { private final ErrorCode errorCode; + private Object data; public ConflictException(ErrorCode errorCode) { super(errorCode.getMessage()); this.errorCode = errorCode; } + + public ConflictException(ErrorCode errorCode, Object data) { + super(errorCode.getMessage()); + this.errorCode = errorCode; + this.data = data; + } }