Skip to content

Commit 0cdfe37

Browse files
authored
[refactor] BusinessException 문자열 사용 제거 및 에러 로깅 개선 (#85)
* [refactor] BusinessException 문자열 사용 제거 및 에러 로깅 개선 * [fix] 테스트 기대 메시지와 에러코드 메시지 불일치 수정 * [fix] NOT_FOLLOWING 에러 메시지 테스트 기대값과 일치하도록 수정
1 parent e810ab2 commit 0cdfe37

18 files changed

Lines changed: 139 additions & 36 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@ out/
3939

4040
### 보안정보
4141
application-secret.yml
42+
*.pem
4243

4344
src/main/generated

src/main/java/com/daramg/server/auth/application/AuthService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class AuthService {
4040

4141
public void signup(SignupRequestDto dto, MultipartFile image){
4242
if (userRepository.existsByEmail(dto.getEmail())) {
43-
throw new BusinessException("중복된 이메일입니다.");
43+
throw new BusinessException(AuthErrorStatus.DUPLICATE_EMAIL);
4444
}
4545
userRepository.findByEmailAndUserStatus(dto.getEmail(), UserStatus.DELETED)
4646
.ifPresent(deletedUser -> {
@@ -50,7 +50,7 @@ public void signup(SignupRequestDto dto, MultipartFile image){
5050
}
5151
});
5252
if (userRepository.existsByNickname(dto.getNickname())){
53-
throw new BusinessException("중복된 닉네임입니다.");
53+
throw new BusinessException(AuthErrorStatus.DUPLICATE_NICKNAME);
5454
}
5555
// TODO: bio, 닉네임에 금칙어 검사
5656
String encodedPassword = passwordEncoder.encode(dto.getPassword());
@@ -117,7 +117,7 @@ public void logout(User user){
117117

118118
public void signOut(User user, com.daramg.server.user.dto.PasswordRequestDto request){
119119
if (!passwordEncoder.matches(request.getPassword(), user.getPassword())) {
120-
throw new BusinessException("비밀번호가 일치하지 않습니다.");
120+
throw new BusinessException(AuthErrorStatus.INVALID_PASSWORD);
121121
}
122122
redisTemplate.delete(user.getEmail());
123123
user.withdraw();

src/main/java/com/daramg/server/auth/application/MailVerificationServiceImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.daramg.server.auth.util.MimeMessageGenerator;
1010
import com.daramg.server.auth.util.VerificationCodeGenerator;
1111
import com.daramg.server.common.exception.BusinessException;
12+
import com.daramg.server.user.exception.UserErrorStatus;
1213
import com.daramg.server.user.repository.UserRepository;
1314
import jakarta.mail.MessagingException;
1415
import jakarta.mail.internet.MimeMessage;
@@ -35,7 +36,7 @@ public void sendVerificationEmail(EmailVerificationRequestDto request) {
3536
case SIGNUP -> sendForSignup(request);
3637
case PASSWORD_RESET -> sendForPasswordReset(request);
3738
case EMAIL_CHANGE -> sendForEmailChange(request);
38-
default -> throw new BusinessException("지원하지 않는 이메일 발송 목적입니다.");
39+
default -> throw new BusinessException(AuthErrorStatus.UNSUPPORTED_EMAIL_PURPOSE);
3940
}
4041
}
4142

@@ -53,7 +54,7 @@ private void sendForPasswordReset(EmailVerificationRequestDto request) {
5354
private void sendForEmailChange(EmailVerificationRequestDto request) {
5455
if (userRepository.existsByEmail(request.getEmail())
5556
&& !request.getEmail().equals(request.getOriginalEmail())) {
56-
throw new BusinessException("이미 가입되어 있는 이메일입니다.");
57+
throw new BusinessException(UserErrorStatus.DUPLICATE_EMAIL);
5758
}
5859
sendVerificationCode(request);
5960
}

src/main/java/com/daramg/server/auth/exception/AuthErrorStatus.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public enum AuthErrorStatus implements BaseErrorCode {
2626

2727
USER_NOT_FOUND_EXCEPTION(HttpStatus.NOT_FOUND, ErrorCategory.AUTH.generate(404_1), "존재하지 않는 사용자입니다."),
2828

29+
DUPLICATE_EMAIL(HttpStatus.CONFLICT, ErrorCategory.AUTH.generate(409_1), "중복된 이메일입니다."),
30+
DUPLICATE_NICKNAME(HttpStatus.CONFLICT, ErrorCategory.AUTH.generate(409_2), "중복된 닉네임입니다."),
31+
INVALID_PASSWORD(HttpStatus.BAD_REQUEST, ErrorCategory.AUTH.generate(400_8), "비밀번호가 일치하지 않습니다."),
32+
UNSUPPORTED_EMAIL_PURPOSE(HttpStatus.BAD_REQUEST, ErrorCategory.AUTH.generate(400_9), "지원하지 않는 이메일 발송 목적입니다."),
33+
2934
SEND_VERIFICATION_EMAIL_FAILED(HttpStatus.INTERNAL_SERVER_ERROR, ErrorCategory.AUTH.generate(500), "이메일 전송에 실패했습니다."),
3035
REDIS_CONNECTION_FAILED(HttpStatus.INTERNAL_SERVER_ERROR, ErrorCategory.AUTH.generate(500_1), "Redis 연결에 실패했습니다. 서버 관리자에게 문의 바랍니다.");
3136

src/main/java/com/daramg/server/comment/application/CommentService.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.daramg.server.comment.repository.CommentLikeRepository;
77
import com.daramg.server.comment.repository.CommentRepository;
88
import com.daramg.server.common.application.EntityUtils;
9+
import com.daramg.server.comment.exception.CommentErrorStatus;
910
import com.daramg.server.common.exception.BusinessException;
1011
import com.daramg.server.post.domain.Post;
1112
import com.daramg.server.notification.domain.NotificationType;
@@ -31,7 +32,7 @@ public class CommentService {
3132
public void createComment(Long postId, CommentCreateDto request, User user){
3233
Post post = entityUtils.getEntity(postId, Post.class);
3334
if (post.isBlocked()){
34-
throw new BusinessException("블락된 포스트에는 댓글을 남길 수 없습니다.");
35+
throw new BusinessException(CommentErrorStatus.BLOCKED_POST);
3536
}
3637

3738
Comment comment = Comment.of(
@@ -53,11 +54,11 @@ public void createComment(Long postId, CommentCreateDto request, User user){
5354
public void createReply(Long commentId, CommentReplyCreateDto request, User user){
5455
Comment parentComment = entityUtils.getEntity(commentId, Comment.class);
5556
if (parentComment.isDeleted() || parentComment.isBlocked()){
56-
throw new BusinessException("삭제되었거나 블락된 댓글에는 대댓글을 남길 수 없습니다.");
57+
throw new BusinessException(CommentErrorStatus.BLOCKED_OR_DELETED_COMMENT_REPLY);
5758
}
5859
Post post = parentComment.getPost();
5960
if (post.isBlocked()){
60-
throw new BusinessException("블락된 포스트에는 댓글을 남길 수 없습니다.");
61+
throw new BusinessException(CommentErrorStatus.BLOCKED_POST);
6162
}
6263

6364
Comment reply = Comment.of(
@@ -79,7 +80,7 @@ public void createReply(Long commentId, CommentReplyCreateDto request, User user
7980
public CommentLikeResponseDto toggleCommentLike(Long commentId, User user){
8081
Comment comment = entityUtils.getEntity(commentId, Comment.class);
8182
if (comment.isDeleted() || comment.isBlocked()){
82-
throw new BusinessException("삭제되었거나 블락된 댓글에는 좋아요를 누를 수 없습니다.");
83+
throw new BusinessException(CommentErrorStatus.BLOCKED_OR_DELETED_COMMENT_LIKE);
8384
}
8485

8586
boolean alreadyLiked = commentLikeRepository
@@ -105,10 +106,10 @@ public void deleteComment(Long commentId, User user){
105106
Comment comment = entityUtils.getEntity(commentId, Comment.class);
106107

107108
if (comment.isDeleted()){
108-
throw new BusinessException("이미 삭제 처리된 댓글입니다.");
109+
throw new BusinessException(CommentErrorStatus.ALREADY_DELETED);
109110
}
110111
if (comment.getUser() == null || !comment.getUser().getId().equals(user.getId())){
111-
throw new BusinessException("댓글을 작성한 유저만 댓글을 삭제할 수 있습니다.");
112+
throw new BusinessException(CommentErrorStatus.NOT_COMMENT_AUTHOR);
112113
}
113114

114115
comment.softDelete();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.daramg.server.comment.exception;
2+
3+
import com.daramg.server.common.exception.BaseErrorCode;
4+
import com.daramg.server.common.exception.ErrorCategory;
5+
import lombok.AccessLevel;
6+
import lombok.AllArgsConstructor;
7+
import lombok.Getter;
8+
import org.springframework.http.HttpStatus;
9+
10+
@Getter
11+
@AllArgsConstructor(access = AccessLevel.PRIVATE)
12+
public enum CommentErrorStatus implements BaseErrorCode {
13+
14+
BLOCKED_POST(HttpStatus.FORBIDDEN, ErrorCategory.COMMENT.generate(403_1), "블락된 포스트에는 댓글을 남길 수 없습니다."),
15+
BLOCKED_OR_DELETED_COMMENT_REPLY(HttpStatus.FORBIDDEN, ErrorCategory.COMMENT.generate(403_2), "삭제되었거나 블락된 댓글에는 대댓글을 남길 수 없습니다."),
16+
BLOCKED_OR_DELETED_COMMENT_LIKE(HttpStatus.FORBIDDEN, ErrorCategory.COMMENT.generate(403_3), "삭제되었거나 블락된 댓글에는 좋아요를 누를 수 없습니다."),
17+
ALREADY_DELETED(HttpStatus.BAD_REQUEST, ErrorCategory.COMMENT.generate(400_1), "이미 삭제 처리된 댓글입니다."),
18+
NOT_COMMENT_AUTHOR(HttpStatus.FORBIDDEN, ErrorCategory.COMMENT.generate(403_4), "댓글을 작성한 유저만 댓글을 삭제할 수 있습니다.");
19+
20+
private final HttpStatus httpStatus;
21+
private final String code;
22+
private final String message;
23+
}

src/main/java/com/daramg/server/common/exception/CommonErrorStatus.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public enum CommonErrorStatus implements BaseErrorCode {
1313
GATEWAY_TIMEOUT(HttpStatus.GATEWAY_TIMEOUT, ErrorCategory.COMMON.generate(504), "타임아웃 에러, 서버 관리자에게 문의 바랍니다."),
1414

1515
BAD_REQUEST(HttpStatus.BAD_REQUEST, ErrorCategory.COMMON.generate(400), "유효하지 않은 요청입니다."),
16+
INVALID_CURSOR(HttpStatus.BAD_REQUEST, ErrorCategory.COMMON.generate(400_1), "유효하지 않은 커서 포맷입니다."),
1617
UNAUTHORIZED(HttpStatus.UNAUTHORIZED, ErrorCategory.COMMON.generate(401), "인증이 필요합니다."),
1718
FORBIDDEN(HttpStatus.FORBIDDEN, ErrorCategory.COMMON.generate(403), "금지된 요청입니다."),
1819
NOT_FOUND(HttpStatus.NOT_FOUND , ErrorCategory.COMMON.generate(404), "찾을 수 없는 리소스입니다.");

src/main/java/com/daramg/server/common/exception/ErrorCategory.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ public enum ErrorCategory {
66
AUTH("AUTH_"),
77
USER("USER_"),
88
POST("POST_"),
9-
IMAGE("IMAGE_");
9+
IMAGE("IMAGE_"),
10+
COMMENT("COMMENT_"),
11+
NOTIFICATION("NOTIFICATION_");
1012

1113
private final String prefix;
1214

src/main/java/com/daramg/server/common/exception/GlobalExceptionHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
2929
public ResponseEntity<ErrorResponse> handleGeneralException(BusinessException e) {
3030
BaseErrorCode errorCode = e.getErrorCode();
3131

32-
log.warn("GeneralException: {}", errorCode.getMessage());
32+
log.warn("GeneralException: {} - {}", errorCode.getMessage(), e.getMessage());
3333
return ResponseEntity
3434
.status(errorCode.getHttpStatus())
3535
.body(ErrorResponse.of(errorCode));

src/main/java/com/daramg/server/common/util/PagingUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.daramg.server.common.dto.PageRequestDto;
44
import com.daramg.server.common.dto.PageResponseDto;
55
import com.daramg.server.common.exception.BusinessException;
6+
import com.daramg.server.common.exception.CommonErrorStatus;
67
import com.querydsl.core.types.dsl.BooleanExpression;
78
import com.querydsl.core.types.dsl.DateTimePath;
89
import com.querydsl.core.types.dsl.NumberPath;
@@ -116,7 +117,7 @@ private Cursor decodeCursor(String cursorString) {
116117
return new Cursor(LocalDateTime.parse(parts[0]), Long.parseLong(parts[1]));
117118
} catch (IllegalArgumentException | ArrayIndexOutOfBoundsException |
118119
DateTimeParseException e) {
119-
throw new BusinessException("유효하지 않은 커서 포맷입니다.");
120+
throw new BusinessException(CommonErrorStatus.INVALID_CURSOR);
120121
}
121122
}
122123

0 commit comments

Comments
 (0)