From 0617f0470a14d91cea4153a2134cbb7484f865f4 Mon Sep 17 00:00:00 2001 From: Hanjaemo <110653660+Hanjaemo@users.noreply.github.com> Date: Fri, 28 Jun 2024 02:21:31 +0900 Subject: [PATCH] =?UTF-8?q?=EC=9A=94=EC=B2=AD=20=EA=B0=92=20=EA=B2=80?= =?UTF-8?q?=EC=A6=9D=20=EB=A1=9C=EC=A7=81=20=EA=B0=9C=EC=84=A0=20(#120)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: 위도, 경도 유효성 검증 실패 시 오류 메시지 개선 * feat: ExceptionHandler 메서드에 에러 로깅 추가 * test: 테스트 메서드명 개선 * feat: 위도, 경도 null 체크 로직 추가 * refactor: 에러 코드 이름 개선 * test: 리팩터링에 따라 발생 예외 타입 수정 --- .../global/exception/ErrorCode.java | 4 ++++ .../exception/GlobalExceptionHandler.java | 10 +++++--- .../module/location/domain/GeoPoint.java | 24 +++++++++++++++---- .../module/collectingbox/domain/TagsTest.java | 2 +- .../module/location/domain/GeoPointTest.java | 9 +++---- 5 files changed, 37 insertions(+), 12 deletions(-) diff --git a/src/main/java/contest/collectingbox/global/exception/ErrorCode.java b/src/main/java/contest/collectingbox/global/exception/ErrorCode.java index 4034358..0d0eebc 100644 --- a/src/main/java/contest/collectingbox/global/exception/ErrorCode.java +++ b/src/main/java/contest/collectingbox/global/exception/ErrorCode.java @@ -16,6 +16,10 @@ public enum ErrorCode { INVALID_BEAN(BAD_REQUEST, "유효하지 않은 데이터입니다."), MISSING_REQUEST_PARAM(BAD_REQUEST, "필수 요청 파라미터가 존재하지 않습니다."), MISMATCH_REQUEST_PARAM(BAD_REQUEST, "요청 파라미터가 유효하지 않습니다."), + OUT_OF_RANGE_LONGITUDE(BAD_REQUEST, "경도는 -180 이상 180 이하의 값이어야 합니다."), + OUT_OF_RANGE_LATITUDE(BAD_REQUEST, "위도는 -90 이상 90 이하의 값이어야 합니다."), + NOT_NULL_LONGITUDE(BAD_REQUEST, "경도 값은 필수입니다."), + NOT_NULL_LATITUDE(BAD_REQUEST, "위도 값은 필수입니다."), // 404 NOT_FOUND_COLLECTING_BOX(NOT_FOUND, "해당 수거함이 존재하지 않습니다."), diff --git a/src/main/java/contest/collectingbox/global/exception/GlobalExceptionHandler.java b/src/main/java/contest/collectingbox/global/exception/GlobalExceptionHandler.java index 2747ee7..f3f7bf0 100644 --- a/src/main/java/contest/collectingbox/global/exception/GlobalExceptionHandler.java +++ b/src/main/java/contest/collectingbox/global/exception/GlobalExceptionHandler.java @@ -18,24 +18,28 @@ public class GlobalExceptionHandler { @ExceptionHandler(CollectingBoxException.class) public ApiResponse handleCollectingBoxException(CollectingBoxException e) { + log.error(e.message()); return ApiResponse.error(e.errorCode(), e.message()); } @ExceptionHandler(MethodArgumentNotValidException.class) - public ApiResponse methodValidException(MethodArgumentNotValidException e) { + public ApiResponse handleMethodArgumentNotValidException(MethodArgumentNotValidException e) { String message = e.getBindingResult().getFieldError().getDefaultMessage(); + log.error(message); return ApiResponse.error(INVALID_BEAN, message); } @ExceptionHandler(MissingServletRequestParameterException.class) @ResponseStatus(BAD_REQUEST) - public ApiResponse handleMissingParams(MissingServletRequestParameterException e) { + public ApiResponse handleMissingServletRequestParameterException(MissingServletRequestParameterException e) { + log.error(e.getMessage()); return ApiResponse.error(MISSING_REQUEST_PARAM, MISSING_REQUEST_PARAM.getMessage()); } @ExceptionHandler(MethodArgumentTypeMismatchException.class) @ResponseStatus(BAD_REQUEST) - public ApiResponse handleMissingParams(MethodArgumentTypeMismatchException e) { + public ApiResponse handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) { + log.error(e.getMessage()); return ApiResponse.error(MISMATCH_REQUEST_PARAM, MISSING_REQUEST_PARAM.getMessage()); } } diff --git a/src/main/java/contest/collectingbox/module/location/domain/GeoPoint.java b/src/main/java/contest/collectingbox/module/location/domain/GeoPoint.java index 58e9f91..f44513e 100644 --- a/src/main/java/contest/collectingbox/module/location/domain/GeoPoint.java +++ b/src/main/java/contest/collectingbox/module/location/domain/GeoPoint.java @@ -1,20 +1,36 @@ package contest.collectingbox.module.location.domain; +import contest.collectingbox.global.exception.CollectingBoxException; import lombok.Getter; +import static contest.collectingbox.global.exception.ErrorCode.*; + @Getter public class GeoPoint { private final double longitude; private final double latitude; - public GeoPoint(double longitude, double latitude) { - if (invalidLongitude(longitude) || invalidLatitude(latitude)) { - throw new IllegalArgumentException("Invalid parameter value for longitude or latitude"); - } + public GeoPoint(Double longitude, Double latitude) { + validate(longitude, latitude); this.longitude = longitude; this.latitude = latitude; } + private void validate(Double longitude, Double latitude) { + if (longitude == null) { + throw new CollectingBoxException(NOT_NULL_LONGITUDE); + } + if (latitude == null) { + throw new CollectingBoxException(NOT_NULL_LATITUDE); + } + if (invalidLongitude(longitude)) { + throw new CollectingBoxException(OUT_OF_RANGE_LONGITUDE); + } + if (invalidLatitude(latitude)) { + throw new CollectingBoxException(OUT_OF_RANGE_LATITUDE); + } + } + private boolean invalidLongitude(double longitude) { return longitude < -180 || longitude > 180; } diff --git a/src/test/java/contest/collectingbox/module/collectingbox/domain/TagsTest.java b/src/test/java/contest/collectingbox/module/collectingbox/domain/TagsTest.java index b132f18..ddfc266 100644 --- a/src/test/java/contest/collectingbox/module/collectingbox/domain/TagsTest.java +++ b/src/test/java/contest/collectingbox/module/collectingbox/domain/TagsTest.java @@ -12,7 +12,7 @@ class TagsTest { @Test @DisplayName("태그가 선택되지 않은 경우 실패한다.") - void fail_byNull() { + void fail_byTagsIsEmpty() { // when, then assertThatThrownBy(() -> new Tags(List.of())) .isInstanceOf(CollectingBoxException.class); diff --git a/src/test/java/contest/collectingbox/module/location/domain/GeoPointTest.java b/src/test/java/contest/collectingbox/module/location/domain/GeoPointTest.java index 3434764..e92fe27 100644 --- a/src/test/java/contest/collectingbox/module/location/domain/GeoPointTest.java +++ b/src/test/java/contest/collectingbox/module/location/domain/GeoPointTest.java @@ -1,5 +1,6 @@ package contest.collectingbox.module.location.domain; +import contest.collectingbox.global.exception.CollectingBoxException; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -12,7 +13,7 @@ class GeoPointTest { void fail_byLongitudeLessThanMin() { // when, then assertThatThrownBy(() -> new GeoPoint(-202.902174727201, 37.4871190365551)) - .isInstanceOf(IllegalArgumentException.class); + .isInstanceOf(CollectingBoxException.class); } @Test @@ -20,7 +21,7 @@ void fail_byLongitudeLessThanMin() { void fail_byLongitudeGreaterThanMax() { // when, then assertThatThrownBy(() -> new GeoPoint(202.902174727201, 37.4871190365551)) - .isInstanceOf(IllegalArgumentException.class); + .isInstanceOf(CollectingBoxException.class); } @Test @@ -28,7 +29,7 @@ void fail_byLongitudeGreaterThanMax() { void fail_byLatitudeLessThanMin() { // when, then assertThatThrownBy(() -> new GeoPoint(126.902174727201, -95.4871190365551)) - .isInstanceOf(IllegalArgumentException.class); + .isInstanceOf(CollectingBoxException.class); } @Test @@ -36,6 +37,6 @@ void fail_byLatitudeLessThanMin() { void fail_byLatitudeGreaterThanMax() { // when, then assertThatThrownBy(() -> new GeoPoint(126.902174727201, 95.4871190365551)) - .isInstanceOf(IllegalArgumentException.class); + .isInstanceOf(CollectingBoxException.class); } } \ No newline at end of file