Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] fix: 학생 인증 코드가 유효하지 않을 시 500이 아닌 400 에러를 반환 (#447) #622

Merged
merged 2 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.festago.student.domain;

import com.festago.common.exception.UnexpectedException;
import com.festago.common.exception.ValidException;
import com.festago.common.util.Validator;
import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import java.util.regex.Pattern;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.util.StringUtils;

@Embeddable
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand All @@ -29,20 +29,18 @@ private void validate(String value) {
}

private void validateBlank(String value) {
if (!StringUtils.hasText(value)) {
throw new UnexpectedException("VerificationCode는 null 또는 공백이 될 수 없습니다.");
}
Validator.hasBlank(value, "VerificationCode");
}

private void validateLength(String value) {
if (value.length() != LENGTH) {
throw new UnexpectedException("VerificationCode의 길이는 %d 이어야 합니다.".formatted(LENGTH));
throw new ValidException("VerificationCode의 길이는 %d 이어야 합니다.".formatted(LENGTH));
}
}

private void validatePositive(String value) {
if (!POSITIVE_REGEX.matcher(value).matches()) {
throw new UnexpectedException("VerificationCode는 0~9의 양수 형식이어야 합니다.");
throw new ValidException("VerificationCode는 0~9의 양수 형식이어야 합니다.");
Comment on lines -39 to +43
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 예외는 ValidException으로 할 지, BadRequest로 할 지 헷갈리네요.
아마 값에 대한 기본적인 예외가 아닌, 도메인 요구 사항에 대한 예외니까 BadRequest가 더 맞아 보이긴 하는데..
의견이 필요합니다.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 BadRequest가 맞다고 느낍니다.
이용자의 입력값에 의한 예외니까 닉네임 4자리 미만인데 3자리 입력 이런 느낌?

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.festago.common.exception.UnexpectedException;
import com.festago.common.exception.ValidException;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores;
import org.junit.jupiter.api.Test;
Expand All @@ -18,32 +18,32 @@ class VerificationCodeTest {
void null_이면_예외() {
// when & then
assertThatThrownBy(() -> new VerificationCode(null))
.isInstanceOf(UnexpectedException.class)
.hasMessage("VerificationCode는 null 또는 공백이 될 수 없습니다.");
.isInstanceOf(ValidException.class)
.hasMessage("VerificationCode은/는 null 또는 공백이 될 수 없습니다.");
}

@ParameterizedTest
@ValueSource(strings = {"12345", "1234567"})
void 길이가_6자리가_아니면_예외(String code) {
// when & then
assertThatThrownBy(() -> new VerificationCode(code))
.isInstanceOf(UnexpectedException.class)
.isInstanceOf(ValidException.class)
.hasMessage("VerificationCode의 길이는 6 이어야 합니다.");
}

@Test
void 숫자가_아니면_예외() {
// when & then
assertThatThrownBy(() -> new VerificationCode("일이삼사오육"))
.isInstanceOf(UnexpectedException.class)
.isInstanceOf(ValidException.class)
.hasMessage("VerificationCode는 0~9의 양수 형식이어야 합니다.");
}

@Test
void 음수이면_예외() {
// when & then
assertThatThrownBy(() -> new VerificationCode("-12345"))
.isInstanceOf(UnexpectedException.class)
.isInstanceOf(ValidException.class)
.hasMessage("VerificationCode는 0~9의 양수 형식이어야 합니다.");
}

Expand Down
Loading