Skip to content

Commit

Permalink
#482 [fix] 리스폰스 에러 코드 분기처리
Browse files Browse the repository at this point in the history
#482 [fix] 리스폰스 에러 코드 분기처리
  • Loading branch information
sohyundoh authored Aug 15, 2024
2 parents 4d10ed1 + 3a59c16 commit a6bc4bc
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected void init() {
}

private String getTokenFromHeader(final String token) {
if (!StringUtils.hasText(token)) {
if (!StringUtils.hasText(token) || token.equals("Bearer null") || token.equals("Bearer ")) {
throw new UnauthorizedException(ErrorMessage.UN_LOGIN_EXCEPTION);
} else if (StringUtils.hasText(token) && !token.startsWith("Bearer ")) {
throw new BadRequestException(ErrorMessage.BEARER_LOST_ERROR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public enum ErrorMessage {
MOIM_OWNER_AUTHENTICATION_ERROR(40303, "사용자는 해당 모임의 모임장이 아닙니다."),
WRITER_NAME_INFO_FORBIDDEN(HttpStatus.FORBIDDEN.value(), "해당 사용자는 필명에 접근 권한이 없습니다."),
COMMENT_ACCESS_ERROR(40305, "해당 사용자는 댓글에 접근 권한이 없습니다."),
WRITER_NAME_NON_AUTHENTICATE(40306, "사용자는 모임에 대한 접근 권한이 없습니다.(Non Error)"),
/*
Method Not Supported
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public List<CommentResponse> getCommentResponse(
final Post post,
final Long userId
) {
postRetriever.authenticateUserWithPostId(post.getId(), userId);
postRetriever.authenticateUserWithPost(post, userId);
List<Comment> commentList = commentRetriever.findByPostId(post.getId());
Long writerNameId = writerNameRetriever.getWriterNameIdByMoimIdAndUserId(moimId, userId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void authenticateUserWithPost(
final Long userId
) {
Long moimId = post.getTopic().getMoim().getId();
authenticateUserOfMoim(writerNameRetriever.isUserInMoim(moimId, userId));
writerNameRetriever.findByMoimAndUserWithNotExceptionCase(moimId, userId);
}

public void authenticateUserOfMoim(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void createCommentOnPost(
) {
Post post = postRetriever.findById(postId);
final Long moimId = post.getTopic().getMoim().getId();
postRetriever.authenticateUserOfMoim(writerNameRetriever.isUserInMoim(moimId, userId));
postRetriever.authenticateUserWithPost(post, userId);
commentCreator.createComment(post, writerNameRetriever.findByMoimAndUser(moimId, userId), commentCreateRequest);
}

Expand All @@ -84,7 +84,7 @@ public PostCuriousResponse createCuriousOnPost(
) {
Post post = postRetriever.findById(postId);
Long moimId = post.getTopic().getMoim().getId();
postRetriever.authenticateUserOfMoim(writerNameRetriever.isUserInMoim(moimId, userId));
postRetriever.authenticateUserWithPost(post, userId);
curiousService.createCurious(post, writerNameRetriever.findByMoimAndUser(moimId, userId));
return PostCuriousResponse.of(CURIOUS_TRUE);
}
Expand Down Expand Up @@ -113,7 +113,7 @@ public PostCuriousResponse deleteCuriousOnPost(
final Long userId
) {
Post post = postRetriever.findById(postId);
curiousService.deleteCurious(post, writerNameRetriever.findByMoimAndUser(post.getTopic().getMoim().getId(), userId));
curiousService.deleteCurious(post, writerNameRetriever.findByMoimAndUserWithNotExceptionCase(post.getTopic().getMoim().getId(), userId));
return PostCuriousResponse.of(CURIOUS_FALSE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public WriterNameShortResponse findWriterNameInfo(
final Long moimId,
final Long userId
) {
return WriterNameShortResponse.of(findByMoimAndUser(moimId, userId));
return WriterNameShortResponse.of(findByMoimAndUserWithNotExceptionCase(moimId, userId));
}

public boolean isUserInMoim(
Expand All @@ -70,6 +70,15 @@ public WriterName findByMoimAndUser(
);
}

public WriterName findByMoimAndUserWithNotExceptionCase(
final Long moimId,
final Long writerId
) {
return writerNameRepository.findByMoimIdAndWriterId(moimId, writerId)
.orElseThrow( () -> new ForbiddenException(ErrorMessage.WRITER_NAME_NON_AUTHENTICATE)
);
}

public Long getWriterNameIdByMoimIdAndUserId(
final Long moimId,
final Long userId
Expand Down

0 comments on commit a6bc4bc

Please sign in to comment.