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

Feat/1441 표준 예외 커스텀화 #1442

Merged
merged 5 commits into from
Jul 23, 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
48 changes: 0 additions & 48 deletions backend/.ebextensions/02-set-filebeat.config

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public enum BadRequestCode {
SCRAP_NOT_EXIST(3008, "스크랩이 존재하지 않습니다."),
SCRAP_NOT_VALID_USER(3009, "본인의 스크랩만 추가할 수 있습니다."),
NOT_EXISTS_MEMBER_TAG(3010, "멤버 태그가 존재하지 않습니다."),
CANT_FIND_GROUP_TYPE(3011, "해당 그룹의 타입을 결정할 수 없습니다."),

INVALID_LIKE_REQUEST_EXCEPTION(5001, "스터디로그를 좋아요 할 수 없습니다."),
INVALID_UNLIKE_REQUEST_EXCEPTION(5002, "스터디로그를 좋아요 취소 할 수 없습니다."),
Expand All @@ -65,6 +66,10 @@ public enum BadRequestCode {
CURRICULUM_NOT_FOUND_EXCEPTION(8010, "해당하는 커리큘럼을 찾을 수 없습니다"),
CURRICULUM_INVALID_EXCEPTION(8011, "커리큘럼이 유효하지 않습니다."),

ESSAY_ANSWER_NOT_FOUND_EXCEPTION(8012, "해당 답변을 찾을 수 없습니다."),
NOT_EMPTY_ESSAY_ANSWER_EXCEPTION(8013, "답변은 공백일 수 없습니다."),
ESSAY_ANSWER_NOT_VALID_USER(8014, "본인이 작성한 답변만 수정할 수 있습니다."),

FILE_NAME_EMPTY_EXCEPTION(9001, "파일 이름이 존재하지 않습니다."),
UNSUPPORTED_FILE_EXTENSION_EXCEPTION(9002, "지원하지 않는 파일 확장자입니다."),
FILE_UPLOAD_FAIL_EXCEPTION(9003, "파일 업로드에 실패했습니다."),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package wooteco.prolog.member.domain;

import static wooteco.prolog.common.exception.BadRequestCode.CANT_FIND_GROUP_TYPE;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import wooteco.prolog.common.exception.BadRequestCode;
import wooteco.prolog.common.exception.BadRequestException;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand All @@ -31,13 +35,13 @@ public MemberGroup(Long id, String name, String description) {
this.description = description;
}

public MemberGroupType getGroupType() {
public MemberGroupType groupType() {
for (MemberGroupType groupType : MemberGroupType.values()) {
if (groupType.isContainedBy(this.name)) {
return groupType;
}
}
throw new IllegalArgumentException("그룹이 포함되는 타입이 없습니다. id=" + this.id);
throw new BadRequestException(CANT_FIND_GROUP_TYPE);
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package wooteco.prolog.roadmap.application;

import static wooteco.prolog.common.exception.BadRequestCode.ESSAY_ANSWER_NOT_FOUND_EXCEPTION;
import static wooteco.prolog.common.exception.BadRequestCode.ROADMAP_QUIZ_NOT_FOUND_EXCEPTION;

import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import wooteco.prolog.common.exception.BadRequestException;
import wooteco.prolog.member.application.MemberService;
import wooteco.prolog.member.domain.Member;
import wooteco.prolog.roadmap.application.dto.EssayAnswerRequest;
Expand Down Expand Up @@ -36,7 +40,7 @@ public EssayAnswerService(EssayAnswerRepository essayAnswerRepository,
public Long createEssayAnswer(EssayAnswerRequest essayAnswerRequest, Long memberId) {
Long quizId = essayAnswerRequest.getQuizId();
Quiz quiz = quizRepository.findById(quizId)
.orElseThrow(() -> new IllegalArgumentException("퀴즈가 존재하지 않습니다. quizId=" + quizId));
.orElseThrow(() -> new BadRequestException(ROADMAP_QUIZ_NOT_FOUND_EXCEPTION));

Member member = memberService.findById(memberId);
EssayAnswer essayAnswer = new EssayAnswer(quiz, essayAnswerRequest.getAnswer(), member);
Expand All @@ -56,16 +60,15 @@ public void updateEssayAnswer(Long answerId, EssayAnswerUpdateRequest request, L

@Transactional
public void deleteEssayAnswer(Long answerId, Long memberId) {
if (!essayAnswerRepository.findByIdAndMemberId(answerId, memberId).isPresent()) {
throw new IllegalArgumentException("답변이 존재하지 않습니다. answerId=" + answerId);
}
essayAnswerRepository.deleteById(answerId);
EssayAnswer essayAnswer = essayAnswerRepository.findByIdAndMemberId(answerId, memberId)
.orElseThrow(() -> new BadRequestException(ESSAY_ANSWER_NOT_FOUND_EXCEPTION));
essayAnswerRepository.deleteById(essayAnswer.getId());
}

@Transactional(readOnly = true)
public EssayAnswer getById(Long answerId) {
EssayAnswer essayAnswer = essayAnswerRepository.findById(answerId)
.orElseThrow(() -> new IllegalArgumentException("답변이 존재하지 않습니다. answerId=" + answerId));
.orElseThrow(() -> new BadRequestException(ESSAY_ANSWER_NOT_FOUND_EXCEPTION));
Hibernate.initialize(essayAnswer.getQuiz());
Hibernate.initialize(essayAnswer.getMember());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package wooteco.prolog.roadmap.domain;

import static wooteco.prolog.common.exception.BadRequestCode.ESSAY_ANSWER_NOT_VALID_USER;
import static wooteco.prolog.common.exception.BadRequestCode.NOT_EMPTY_ESSAY_ANSWER_EXCEPTION;

import javax.persistence.Column;
import javax.persistence.ConstraintMode;
import javax.persistence.Entity;
Expand All @@ -16,6 +19,7 @@
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import wooteco.prolog.common.AuditingEntity;
import wooteco.prolog.common.exception.BadRequestException;
import wooteco.prolog.member.domain.Member;

@Table(name = "essay_answer")
Expand Down Expand Up @@ -52,10 +56,10 @@ public EssayAnswer(final Quiz quiz, final String answer, Member member) {

public void update(String answer, Member member) {
if (StringUtils.isBlank(answer)) {
throw new IllegalArgumentException("답변 내용은 공백일 수 없습니다.");
throw new BadRequestException(NOT_EMPTY_ESSAY_ANSWER_EXCEPTION);
}
if (!this.member.equals(member)) {
throw new IllegalArgumentException("본인이 작성한 답변만 수정할 수 있습니다.");
throw new BadRequestException(ESSAY_ANSWER_NOT_VALID_USER);
}

this.answer = answer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void updatePopularStudylogs(Pageable pageable) {
List<GroupMember> groupMembers = groupMemberRepository.findAll();
Map<MemberGroupType, List<MemberGroup>> memberGroupsBygroupType = memberGroupRepository.findAll()
.stream()
.collect(Collectors.groupingBy(MemberGroup::getGroupType));
.collect(Collectors.groupingBy(MemberGroup::groupType));

final List<Studylog> recentStudylogs = findRecentStudylogs(LocalDateTime.now(),
pageable.getPageSize());
Expand Down Expand Up @@ -111,7 +111,7 @@ public PopularStudylogsResponse findPopularStudylogs(Pageable pageable, Long mem
List<Studylog> allPopularStudylogs = getSortedPopularStudyLogs(pageable);
List<GroupMember> groupedMembers = groupMemberRepository.findAll();
Map<MemberGroupType, List<MemberGroup>> memberGroupsBygroupType = memberGroupRepository.findAll()
.stream().collect(Collectors.groupingBy(MemberGroup::getGroupType));
.stream().collect(Collectors.groupingBy(MemberGroup::groupType));

return PopularStudylogsResponse.of(
studylogsResponse(allPopularStudylogs, pageable, memberId),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;

import org.junit.jupiter.api.Test;
import wooteco.prolog.common.exception.BadRequestCode;
import wooteco.prolog.common.exception.BadRequestException;

class MemberGroupTest {

Expand All @@ -13,18 +15,18 @@ class MemberGroupTest {

@Test
void getGroupType_이름이_그룹명을_포함하면_그룹을_반환한다() {
assertThat(ANDROID_GROUP.getGroupType()).isEqualTo(MemberGroupType.ANDROID);
assertThat(BACKEND_GROUP.getGroupType()).isEqualTo(MemberGroupType.BACKEND);
assertThat(FRONTEND_GROUP.getGroupType()).isEqualTo(MemberGroupType.FRONTEND);
assertThat(ANDROID_GROUP.groupType()).isEqualTo(MemberGroupType.ANDROID);
assertThat(BACKEND_GROUP.groupType()).isEqualTo(MemberGroupType.BACKEND);
assertThat(FRONTEND_GROUP.groupType()).isEqualTo(MemberGroupType.FRONTEND);
}

@Test
void getGroupType_이름이_포함하는_그룹명이_없으면_예외가_발생한다() {
MemberGroup memberGroup = new MemberGroup(null, "테스트", "test");

assertThatThrownBy(memberGroup::getGroupType)
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("그룹이 포함되는 타입이 없습니다. id=null");
assertThatThrownBy(memberGroup::groupType)
.isInstanceOf(BadRequestException.class)
.hasMessage("해당 그룹의 타입을 결정할 수 없습니다.");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import wooteco.prolog.common.exception.BadRequestCode;
import wooteco.prolog.common.exception.BadRequestException;
import wooteco.prolog.member.application.MemberService;
import wooteco.prolog.member.domain.Member;
Expand Down Expand Up @@ -109,7 +110,7 @@ void deleteEssayAnswer_fail() {

//when,then
assertThatThrownBy(() -> essayAnswerService.deleteEssayAnswer(1L, 1L))
.isInstanceOf(IllegalArgumentException.class);
.isInstanceOf(BadRequestException.class);
}

@DisplayName("deleteEssayAnswer 에서 answerId memberId 에 매핑되는 EssayAnswer 가 있다면 삭제한다")
Expand All @@ -135,7 +136,7 @@ void getById_fail() {

//when,then
assertThatThrownBy(() -> essayAnswerService.getById(1L))
.isInstanceOf(IllegalArgumentException.class);
.isInstanceOf(BadRequestException.class);
}

@DisplayName("getById 에서 answerId 에 해당하는 EssayAnswer 가있다면 반환해준다")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import wooteco.prolog.common.exception.BadRequestCode;
import wooteco.prolog.common.exception.BadRequestException;
import wooteco.prolog.member.domain.Member;

public class EssayAnswerTest {
class EssayAnswerTest {

@ValueSource(strings = {"", " "})
@ParameterizedTest
Expand All @@ -19,8 +21,8 @@ public class EssayAnswerTest {

//expect
assertThatThrownBy(() -> essayAnswer.update(answer, member))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("답변 내용은 공백일 수 없습니다.");
.isInstanceOf(BadRequestException.class)
.hasMessage("답변은 공백일 수 없습니다.");
}

@Test
Expand All @@ -32,7 +34,7 @@ public class EssayAnswerTest {

//expect
assertThatThrownBy(() -> essayAnswer.update("answer", another))
.isInstanceOf(IllegalArgumentException.class)
.isInstanceOf(BadRequestException.class)
.hasMessage("본인이 작성한 답변만 수정할 수 있습니다.");
}

Expand Down
Loading