Skip to content

Commit 2ed8c24

Browse files
authored
Merge pull request #153 from SWU-Elixir/refactor/18-error-handling
refactor: 누락된 에러 처리 보완 및 ErrorCode 통일
2 parents 0735bf7 + 2f96a65 commit 2ed8c24

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

src/main/java/BE_Elixir/Elixir/domain/dietLog/service/DietLogService.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import BE_Elixir.Elixir.domain.achievement.service.MemberStatsService;
44
import BE_Elixir.Elixir.domain.challenge.event.events.DietLogEvent;
5-
import BE_Elixir.Elixir.domain.challenge.event.events.RecipeEvent;
65
import BE_Elixir.Elixir.domain.dietLog.dto.DietLogRequestDTO;
76
import BE_Elixir.Elixir.domain.dietLog.dto.DietLogResponseDTO;
87
import BE_Elixir.Elixir.domain.dietLog.dto.MonthlyDietScoreDTO;
@@ -52,7 +51,7 @@ public DietLogResponseDTO createDietLog(DietLogRequestDTO dto, Long memberId, Mu
5251

5352
// 회원 조회
5453
Member member = memberRepository.findById(memberId)
55-
.orElseThrow(() -> new IllegalArgumentException("회원이 존재하지 않습니다. member id: " + memberId));
54+
.orElseThrow(() -> new CustomException(ErrorCode.MEMBER_NOT_FOUND));
5655

5756
// 식단 타입을 enum 타입으로 변환 및 검사
5857
DietLogType typeEnum = DietLogType.valueOf(dto.getType().toUpperCase());
@@ -98,11 +97,11 @@ public DietLogResponseDTO createDietLog(DietLogRequestDTO dto, Long memberId, Mu
9897
public void deleteDietLog(Long dietLogId, Long memberId) {
9998
// 식단 기록 객체 찾기
10099
DietLog dietLog = dietLogRepository.findById(dietLogId)
101-
.orElseThrow(() -> new IllegalArgumentException("해당 식단이 존재하지 않습니다. 식단 ID: " + dietLogId));
100+
.orElseThrow(() -> new CustomException(ErrorCode.DIETLOG_NOT_FOUND));
102101

103102
// 권한 확인: 본인만 삭제 가능
104103
if (!dietLog.getMember().getId().equals(memberId)) {
105-
throw new SecurityException("해당 식단을 삭제할 권한이 없습니다.");
104+
throw new CustomException(ErrorCode.FORBIDDEN_ACCESS);
106105
}
107106

108107
// S3 버킷에서 이미지 삭제
@@ -117,11 +116,11 @@ public void deleteDietLog(Long dietLogId, Long memberId) {
117116
public DietLogResponseDTO updateDietLog(Long dietLogId, Long memberId, DietLogRequestDTO dto, MultipartFile image) {
118117
// 기존 식단 조회
119118
DietLog dietLog = dietLogRepository.findById(dietLogId)
120-
.orElseThrow(() -> new IllegalArgumentException("해당 식단이 존재하지 않습니다. 식단 ID: " + dietLogId));
119+
.orElseThrow(() -> new CustomException(ErrorCode.DIETLOG_NOT_FOUND));
121120

122121
// 권한 확인: 본인만 수정 가능
123122
if (!dietLog.getMember().getId().equals(memberId)) {
124-
throw new SecurityException("해당 식단을 수정할 권한이 없습니다.");
123+
throw new CustomException(ErrorCode.FORBIDDEN_ACCESS);
125124
}
126125

127126
// 이름 수정
@@ -192,7 +191,7 @@ public DietLogResponseDTO getDietLog(Long dietLogId) {
192191

193192
// 식단 기록 객체 찾기
194193
DietLog dietLog = dietLogRepository.findById(dietLogId)
195-
.orElseThrow(() -> new IllegalArgumentException("해당 식단이 존재하지 않습니다. 식단 ID: " + dietLogId));
194+
.orElseThrow(() -> new CustomException(ErrorCode.DIETLOG_NOT_FOUND));
196195

197196
return dietLog.convertToResponseDTO();
198197

@@ -258,7 +257,7 @@ private void validateDuplicateDietLogType(Long memberId, DietLogType typeEnum, L
258257
);
259258

260259
if (exists) {
261-
throw new CustomException(ErrorCode.DIET_LOG_TYPE_DUPLICATE);
260+
throw new CustomException(ErrorCode.DIETLOG_TYPE_DUPLICATE);
262261
}
263262
}
264263

@@ -276,7 +275,7 @@ private void validateDuplicateDietLogType(Long memberId, DietLogType typeEnum, L
276275
);
277276

278277
if (exists) {
279-
throw new CustomException(ErrorCode.DIET_LOG_TYPE_DUPLICATE);
278+
throw new CustomException(ErrorCode.DIETLOG_TYPE_DUPLICATE);
280279
}
281280
}
282281

src/main/java/BE_Elixir/Elixir/global/exception/ErrorCode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public enum ErrorCode {
4949
// 식단
5050
DIETLOG_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "식단 기록이 존재하지 않습니다."),
5151
INVALID_DIETLOG_TYPE(HttpStatus.BAD_REQUEST.value(), "잘못된 식단 타입(아침, 점심 등)입니다."),
52-
DIET_LOG_TYPE_DUPLICATE(HttpStatus.BAD_REQUEST.value(), "오늘은 이미 해당 식사 유형을 기록하셨습니다."),
52+
DIETLOG_TYPE_DUPLICATE(HttpStatus.BAD_REQUEST.value(), "오늘은 이미 해당 식사 유형을 기록하셨습니다."),
5353

5454
// 레시피
5555
RECIPE_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "레시피가 존재하지 않습니다."),

0 commit comments

Comments
 (0)