Skip to content

Commit 490b377

Browse files
committed
refactor: 예외처리 방식 각 Domain에 알맞게 수정
1 parent 7ea9ed1 commit 490b377

File tree

8 files changed

+99
-15
lines changed

8 files changed

+99
-15
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.example.log4u.domain.diary.exception;
2+
3+
import org.springframework.http.HttpStatus;
4+
5+
import com.example.log4u.common.exception.base.ErrorCode;
6+
7+
import lombok.Getter;
8+
import lombok.RequiredArgsConstructor;
9+
10+
@Getter
11+
@RequiredArgsConstructor
12+
public enum DiaryErrorCode implements ErrorCode {
13+
14+
NOT_FOUND_DIARY(HttpStatus.NOT_FOUND, "다이어리를 찾을 수 없습니다.");
15+
16+
private final HttpStatus httpStatus;
17+
private final String message;
18+
19+
@Override
20+
public HttpStatus getHttpStatus() {
21+
return httpStatus;
22+
}
23+
24+
@Override
25+
public String getErrorMessage() {
26+
return message;
27+
}
28+
}
29+
Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
// package com.example.log4u.domain.diary.exception;
2-
//
3-
// public class DiaryException extends ServiceException{
4-
//
5-
// DiaryException(ErrorCode errorCode){
6-
// super(errorCode);
7-
// }
8-
//
9-
// }
1+
package com.example.log4u.domain.diary.exception;
2+
3+
import com.example.log4u.common.exception.base.ErrorCode;
4+
import com.example.log4u.common.exception.base.ServiceException;
5+
6+
public class DiaryException extends ServiceException {
7+
public DiaryException(ErrorCode errorCode) {
8+
super(errorCode);
9+
}
10+
}
11+
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
// package com.example.log4u.domain.diary.exception;
2-
//
3-
// public class NotFoundDiaryException extends DiaryException{
4-
// }
1+
package com.example.log4u.domain.diary.exception;
2+
3+
public class NotFoundDiaryException extends DiaryException {
4+
public NotFoundDiaryException() {
5+
super(DiaryErrorCode.NOT_FOUND_DIARY);
6+
}
7+
}

src/main/java/com/example/log4u/domain/diary/service/DiaryService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.springframework.stereotype.Service;
44

55
import com.example.log4u.domain.diary.entity.Diary;
6+
import com.example.log4u.domain.diary.exception.NotFoundDiaryException;
67
import com.example.log4u.domain.diary.repository.DiaryRepository;
78

89
import lombok.RequiredArgsConstructor;
@@ -16,7 +17,7 @@ public class DiaryService {
1617

1718
public Diary getDiary(Long diaryId) {
1819
return diaryRepository.findById(diaryId)
19-
.orElseThrow(IllegalArgumentException::new);
20+
.orElseThrow(NotFoundDiaryException::new);
2021
}
2122

2223
public Long incrementLikeCount(Long diaryId) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.example.log4u.domain.like.exception;
2+
3+
public class DuplicateLikeException extends LikeException {
4+
public DuplicateLikeException() {
5+
super(LikeErrorCode.DUPLICATE_LIKE);
6+
}
7+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.example.log4u.domain.like.exception;
2+
3+
import org.springframework.http.HttpStatus;
4+
5+
import com.example.log4u.common.exception.base.ErrorCode;
6+
7+
import lombok.Getter;
8+
import lombok.RequiredArgsConstructor;
9+
10+
@Getter
11+
@RequiredArgsConstructor
12+
public enum LikeErrorCode implements ErrorCode {
13+
14+
NOT_FOUND_LIKE(HttpStatus.NOT_FOUND, "좋아요 정보를 찾을 수 없습니다."),
15+
DUPLICATE_LIKE(HttpStatus.BAD_REQUEST, "이미 좋아요를 눌렀습니다.");
16+
17+
18+
private final HttpStatus httpStatus;
19+
private final String message;
20+
21+
@Override
22+
public HttpStatus getHttpStatus() {
23+
return httpStatus;
24+
}
25+
26+
@Override
27+
public String getErrorMessage() {
28+
return message;
29+
}
30+
}
31+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.example.log4u.domain.like.exception;
2+
3+
import com.example.log4u.common.exception.base.ErrorCode;
4+
import com.example.log4u.common.exception.base.ServiceException;
5+
6+
public class LikeException extends ServiceException {
7+
public LikeException(ErrorCode errorCode) {
8+
super(errorCode);
9+
}
10+
}

src/main/java/com/example/log4u/domain/like/service/LikeService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.example.log4u.domain.like.dto.request.LikeAddRequestDto;
88
import com.example.log4u.domain.like.dto.response.LikeAddResponseDto;
99
import com.example.log4u.domain.like.entity.Like;
10+
import com.example.log4u.domain.like.exception.DuplicateLikeException;
1011
import com.example.log4u.domain.like.repository.LikeRepository;
1112

1213
import lombok.RequiredArgsConstructor;
@@ -31,7 +32,7 @@ public LikeAddResponseDto addLike(Long userId, LikeAddRequestDto requestDto) {
3132

3233
private void validateDuplicateLike(Long userId, Long diaryId) {
3334
if (likeRepository.existsByUserIdAndDiaryId(userId, diaryId)) {
34-
throw new IllegalArgumentException();
35+
throw new DuplicateLikeException();
3536
}
3637
}
3738
}

0 commit comments

Comments
 (0)