Skip to content

Commit

Permalink
요청 값 검증 로직 개선 (#120)
Browse files Browse the repository at this point in the history
* refactor: 위도, 경도 유효성 검증 실패 시 오류 메시지 개선

* feat: ExceptionHandler 메서드에 에러 로깅 추가

* test: 테스트 메서드명 개선

* feat: 위도, 경도 null 체크 로직 추가

* refactor: 에러 코드 이름 개선

* test: 리팩터링에 따라 발생 예외 타입 수정
  • Loading branch information
Hanjaemo authored Jun 27, 2024
1 parent 0281799 commit 0617f04
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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, "해당 수거함이 존재하지 않습니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,28 @@ public class GlobalExceptionHandler {

@ExceptionHandler(CollectingBoxException.class)
public ApiResponse<Object> handleCollectingBoxException(CollectingBoxException e) {
log.error(e.message());
return ApiResponse.error(e.errorCode(), e.message());
}

@ExceptionHandler(MethodArgumentNotValidException.class)
public ApiResponse<Object> methodValidException(MethodArgumentNotValidException e) {
public ApiResponse<Object> 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<Object> handleMissingParams(MissingServletRequestParameterException e) {
public ApiResponse<Object> 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<Object> handleMissingParams(MethodArgumentTypeMismatchException e) {
public ApiResponse<Object> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) {
log.error(e.getMessage());
return ApiResponse.error(MISMATCH_REQUEST_PARAM, MISSING_REQUEST_PARAM.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TagsTest {

@Test
@DisplayName("태그가 선택되지 않은 경우 실패한다.")
void fail_byNull() {
void fail_byTagsIsEmpty() {
// when, then
assertThatThrownBy(() -> new Tags(List.of()))
.isInstanceOf(CollectingBoxException.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -12,30 +13,30 @@ class GeoPointTest {
void fail_byLongitudeLessThanMin() {
// when, then
assertThatThrownBy(() -> new GeoPoint(-202.902174727201, 37.4871190365551))
.isInstanceOf(IllegalArgumentException.class);
.isInstanceOf(CollectingBoxException.class);
}

@Test
@DisplayName("경도가 180보다 큰 경우 실패한다.")
void fail_byLongitudeGreaterThanMax() {
// when, then
assertThatThrownBy(() -> new GeoPoint(202.902174727201, 37.4871190365551))
.isInstanceOf(IllegalArgumentException.class);
.isInstanceOf(CollectingBoxException.class);
}

@Test
@DisplayName("위도가 -90보다 작은 경우 실패한다.")
void fail_byLatitudeLessThanMin() {
// when, then
assertThatThrownBy(() -> new GeoPoint(126.902174727201, -95.4871190365551))
.isInstanceOf(IllegalArgumentException.class);
.isInstanceOf(CollectingBoxException.class);
}

@Test
@DisplayName("위도가 90보다 큰 경우 실패한다.")
void fail_byLatitudeGreaterThanMax() {
// when, then
assertThatThrownBy(() -> new GeoPoint(126.902174727201, 95.4871190365551))
.isInstanceOf(IllegalArgumentException.class);
.isInstanceOf(CollectingBoxException.class);
}
}

0 comments on commit 0617f04

Please sign in to comment.