Skip to content

Commit 7cd0c15

Browse files
committed
fix: 에러 핸들러 수정
1 parent b065dce commit 7cd0c15

File tree

5 files changed

+34
-12
lines changed

5 files changed

+34
-12
lines changed

clokey-api/src/main/java/org/clokey/domain/category/exception/CategoryErrorCode.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
@Getter
99
@AllArgsConstructor
1010
public enum CategoryErrorCode implements BaseErrorCode {
11-
CATEGORY_NOT_FOUND(404, "존재하지 않는 카테고리입니다."),
12-
CATEGORY_IN_BULK_NOT_FOUND(404, "존재하지 않는 카테고리가 포함되어 있습니다.");
11+
CATEGORY_NOT_FOUND(404, "CATEGORY_4041", "존재하지 않는 카테고리입니다."),
12+
CATEGORY_IN_BULK_NOT_FOUND(404, "CATEGORY_4042", "존재하지 않는 카테고리가 포함되어 있습니다.");
1313

1414
private int status;
15+
private String code;
1516
private String message;
1617

1718
@Override
1819
public ErrorReasonDto getErrorReason() {
19-
return null;
20+
return ErrorReasonDto.of(status, code, message);
2021
}
2122
}

clokey-api/src/main/java/org/clokey/domain/category/repository/CategoryRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
public interface CategoryRepository extends JpaRepository<Category, Long> {
88

9-
boolean existsByIdIn(Iterable<Long> ids);
9+
long countByIdIn(Iterable<Long> ids);
1010

1111
List<Category> findAllById(Iterable<Long> ids);
1212
}

clokey-api/src/main/java/org/clokey/domain/cloth/service/ClothServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public ClothCreateResponse createCloths(ClothCreateRequests request) {
5656
}
5757

5858
private Map<Long, Category> getCategoryMapByIds(Set<Long> ids) {
59-
if (!categoryRepository.existsByIdIn(ids)) {
59+
if (categoryRepository.countByIdIn(ids) != ids.size()) {
6060
throw new BaseCustomException(CategoryErrorCode.CATEGORY_IN_BULK_NOT_FOUND);
6161
}
6262

clokey-api/src/test/java/org/clokey/domain/cloth/service/ClothServiceTest.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
package org.clokey.domain.cloth.service;
22

33
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
45
import static org.junit.jupiter.api.Assertions.*;
56
import static org.mockito.BDDMockito.given;
67

78
import java.util.List;
89
import org.clokey.IntegrationTest;
910
import org.clokey.category.entity.Category;
11+
import org.clokey.domain.category.exception.CategoryErrorCode;
1012
import org.clokey.domain.category.repository.CategoryRepository;
1113
import org.clokey.domain.cloth.dto.request.ClothCreateRequest;
1214
import org.clokey.domain.cloth.dto.request.ClothCreateRequests;
1315
import org.clokey.domain.cloth.repository.ClothRepository;
1416
import org.clokey.domain.member.repository.MemberRepository;
17+
import org.clokey.exception.BaseCustomException;
1518
import org.clokey.global.FakeAuthContext;
1619
import org.clokey.member.entity.Member;
1720
import org.clokey.member.entity.OauthInfo;
@@ -73,12 +76,27 @@ void setUp() {
7376
Assertions.assertAll(
7477
() ->
7578
assertThat(clothRepository.findById(1L).orElseThrow())
76-
.extracting("id", "clothImageUrl", "category.id", "member.id")
77-
.containsExactly(1L, "testClothImageUrl1", 1L, 1L),
79+
.extracting("lothImageUrl", "category.id", "member.id")
80+
.containsExactly("testClothImageUrl1", 1L, 1L),
7881
() ->
7982
assertThat(clothRepository.findById(2L).orElseThrow())
80-
.extracting("id", "clothImageUrl", "category.id", "member.id")
81-
.containsExactly(1L, "testClothImageUrl2", 1L, 1L));
83+
.extracting("clothImageUrl", "category.id", "member.id")
84+
.containsExactly("testClothImageUrl2", 1L, 1L));
85+
}
86+
87+
@Test
88+
void 카테고리가_존재하지_않을_경우_예외가_발생한다() {
89+
// given
90+
ClothCreateRequests request =
91+
new ClothCreateRequests(
92+
List.of(
93+
new ClothCreateRequest("testClothImageUrl1", 1L),
94+
new ClothCreateRequest("testClothImageUrl2", 999L)));
95+
96+
// when & then
97+
assertThatThrownBy(() -> clothService.createCloths(request))
98+
.isInstanceOf(BaseCustomException.class)
99+
.hasMessage(CategoryErrorCode.CATEGORY_IN_BULK_NOT_FOUND.getMessage());
82100
}
83101
}
84102
}

clokey-common/src/main/java/org/clokey/exception/BaseCustomException.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package org.clokey.exception;
22

3-
import lombok.AllArgsConstructor;
43
import lombok.Getter;
54
import org.clokey.dto.ErrorReasonDto;
65

76
@Getter
8-
@AllArgsConstructor
97
public class BaseCustomException extends RuntimeException {
108

11-
private BaseErrorCode code;
9+
private final BaseErrorCode code;
10+
11+
public BaseCustomException(BaseErrorCode code) {
12+
super(code.getErrorReason().message());
13+
this.code = code;
14+
}
1215

1316
public ErrorReasonDto getErrorReasonDto() {
1417
return this.code.getErrorReason();

0 commit comments

Comments
 (0)