Exceptions Handling
✅ Mid-Feedback results
1️⃣ What if there are 100 users buying the same item at the same time?
2️⃣ What if there is 0 product left?
3️⃣ What if the product is not sold anymore?
4️⃣ What if the user wants to buy more items than stock?
5️⃣ What if the user has no money, but wants to buy?
✔️ Service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
@Service
@RequiredArgsConstructor
public class OrderService {
@Transactional
public ResponseDto cartToOrder(CustomUserDetails customUserDetails) {
//...other methods
//재고 예외처리 method
List<OptionQuantityDto> optionQuantityList = cartList.stream().map(c-> new OptionQuantityDto(c.getOptions().getOptionsId(), c.getQuantity()))
.collect(Collectors.toList());
exceptionCheck(optionQuantityList);
if(!cartList.isEmpty()) {
//save order JPA
//order response DTO
return new ResponseDto(HttpStatus.OK.value(), "order page show success", orderResponseDto);
}else{
throw new EmptyException("유저의 장바구니가 비었습니다.");
}
}
//주문에서 결제로
public ResponseDto orderToPay(CustomUserDetails customUserDetails, PayRequestDto payRequestDto) {
List<OptionQuantityDto> optionQuantityList= payRequestDto.getOptionQuantityDto();
//재고 예외처리
exceptionCheck(optionQuantityList);
//사용자가 가진 돈이 주문하려는 금액보다 적으면 예외처리
User user = userRepository.findById(customUserDetails.getUserId())
.orElseThrow(() -> new NotFoundException("아이디가 " + customUserDetails.getUserId() + "인 유저를 찾을 수 없습니다."));
Integer totalPrice = payRequestDto.getTotalPrice();
if (totalPrice > user.getMoney()) throw new NoMoneyException("You do not have enough money to order");
try {
//pay method
return new ResponseDto(HttpStatus.OK.value(), "주문 성공");
}catch(Exception e){
e.printStackTrace();
return new ResponseDto(HttpStatus.BAD_REQUEST.value(), "주문 실패");
}
}
//재고처리
private void exceptionCheck(List<OptionQuantityDto> optionQuantityDtoList) throws SoldOutException, NotEnoughStockException, ProductStatusException{
for(OptionQuantityDto o: optionQuantityDtoList){
Options options= optionsRepository.findById(o.getOptionId())
.orElseThrow(()-> new NotFoundException("Cannot find option with ID"));
int buyQuantity= o.getQuantity();
int optionQuantity= options.getStock();
if(optionQuantity==0) throw new SoldOutException("This product is sold out");
else if(buyQuantity>optionQuantity) throw new NotEnoughStockException("Not possible. Product: "+ options.getProduct().getProductName() + " Option: "+ options.getOptionsName() + "Only "+ optionQuantity +"products available.");
else if(!options.getProduct().isProductStatus()) throw new ProductStatusException("This product is not available at the moment.");
}
}
}
✔️ ExceptionControllerAdvice
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@RestControllerAdvice
@Slf4j
public class ExceptionControllerAdvice {
//...other exceptions...
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(NotEnoughStockException.class)
public ResponseEntity<ResponseDto> handleNotEnoughStockException(NotEnoughStockException nee){
log.error("Client 요청에 문제가 있어 다음처럼 출력합니다. " + nee.getMessage());
ResponseDto responseDto = new ResponseDto(HttpStatus.BAD_REQUEST.value(), nee.getMessage());
return new ResponseEntity<>(responseDto, HttpStatus.BAD_REQUEST);
}
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(SoldOutException.class)
public ResponseEntity<ResponseDto> handleSoldOutException(SoldOutException soe){
log.error("Client 요청에 문제가 있어 다음처럼 출력합니다. " + soe.getMessage());
ResponseDto responseDto = new ResponseDto(HttpStatus.BAD_REQUEST.value(), soe.getMessage());
return new ResponseEntity<>(responseDto, HttpStatus.BAD_REQUEST);
}
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(ProductStatusException.class)
public ResponseEntity<ResponseDto> handleProductStatusException(ProductStatusException pse){
log.error("Client 요청에 문제가 있어 다음처럼 출력합니다. " + pse.getMessage());
ResponseDto responseDto = new ResponseDto(HttpStatus.BAD_REQUEST.value(), pse.getMessage());
return new ResponseEntity<>(responseDto, HttpStatus.BAD_REQUEST);
}
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(NoMoneyException.class)
public ResponseEntity<ResponseDto> handleNoMoneyException(NoMoneyException nme){
log.error("Client 요청에 문제가 있어 다음처럼 출력합니다. " + nme.getMessage());
ResponseDto responseDto = new ResponseDto(HttpStatus.BAD_REQUEST.value(), nme.getMessage());
return new ResponseEntity<>(responseDto, HttpStatus.BAD_REQUEST);
}
}
⭐️ Result
✔️ cart to order
💡 SoldOutException
💡 ProductStatusException
✔️ order to pay
💡 SoldOutException
💡 NotEnoughStockException
💡 ProductStatusException
💡 NoMoneyException
This post is licensed under CC BY 4.0 by the author.