Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[] feat(order): 주문 - FeignException HttpStatus, 메시지, 코드 파싱 #119

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package radiata.common.exception;

import java.io.IOException;
import java.util.Map;
import lombok.Getter;
import org.springframework.http.HttpStatus;
import radiata.common.message.ExceptionMessage;
Expand All @@ -18,9 +18,15 @@ public BusinessException(HttpStatus httpStatus, String message, String code) {
this.code = code;
}

public BusinessException(ExceptionMessage exceptionMessage) {
public BusinessException(ExceptionMessage exceptionMessage) {
this.httpStatus = exceptionMessage.getHttpStatus();
this.message = exceptionMessage.getMessage();
this.code = exceptionMessage.getCode();
}

public BusinessException(HttpStatus httpStatus, Map<String, String> feignErrorInfo) {
this.httpStatus = httpStatus;
this.message = feignErrorInfo.get("message");
this.code = feignErrorInfo.get("code");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,8 @@ public enum ExceptionMessage {
NOT_EQUALS_PRICE(CONFLICT, "5002", "결제 요청 금액과 주문 금액이 일치하지 않습니다."),
// 주문 취소가 가능하지 않은 주문 상태일 때.
IMPOSSIBLE_CANCEL_ORDER_PAYMENT(CONFLICT, "5003", "주문 취소가 불가한 주문 상태입니다."),
// 주문 실패 했을 때
ORDER_CREATION_FAILED(BAD_REQUEST, "5004", "주문 생성에 실패했습니다."),
// 결제 실패했을 때
ORDER_PAYMENT_FAILED(BAD_REQUEST, "5005", "주문 상품 결제에 실패했습니다."),
// FeignException 메시지, 코드 파싱 실패 시
FEIGN_CLIENT_PARSE_ERROR(CONFLICT, "5004", "FeignException 메시지 파싱 실패"),

/* 쿠폰 6000번대 */

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package radiata.service.order.core.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import feign.FeignException;
import java.util.HashMap;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -37,6 +42,7 @@ public class ProcessService {
private final PaymentClient paymentClient;
private final UserClient userClient;
private final RollbackService rollbackService;
private final ObjectMapper objectMapper;


// 타임세일상품 재고 확인 및 차감 요청
Expand All @@ -46,7 +52,6 @@ public void checkAndDeductTimeSaleProduct(OrderRollbackContext context, OrderIte
int quantity = orderItem.getQuantity();

if (timeSaleProductId != null) {
// TODO - 갯수 넘기면 header Or param으로 날릴 예정
int discountRate = timeSaleProductClient.checkAndDeductTimeSaleProduct(timeSaleProductId,
new TimeSaleProductSaleRequestDto(quantity)).data().discountRate();
context.addDeductedTimeSale(timeSaleProductId, quantity);
Expand All @@ -55,7 +60,8 @@ public void checkAndDeductTimeSaleProduct(OrderRollbackContext context, OrderIte
} catch (FeignException e) {
log.error("[TimeSale-Service FeignException]: Deduct TimeSale Product Request Error");
rollbackService.createOrderItemsRollback(context);
throw new BusinessException(HttpStatus.CONFLICT, e.getMessage(), "5006");
Map<String, String> errorInfo = parseFeignExceptionMessage(e);
throw new BusinessException(HttpStatus.valueOf(e.status()), errorInfo);
}
}

Expand All @@ -70,7 +76,8 @@ public void checkAndDeductStock(OrderRollbackContext context, OrderItem orderIte
} catch (FeignException e) {
log.error("[Brand(Product)-Service FeignException]: DeductStock Request Error");
rollbackService.createOrderItemsRollback(context);
throw new BusinessException(HttpStatus.CONFLICT, e.getMessage(), "5007");
Map<String, String> errorInfo = parseFeignExceptionMessage(e);
throw new BusinessException(HttpStatus.valueOf(e.status()), errorInfo);
}
}

Expand All @@ -91,7 +98,8 @@ public void checkAndUseCoupon(OrderRollbackContext context, String userId, Order
} catch (FeignException e) {
log.error("[Coupon-Service FeignException]: UseCoupon Request Error");
rollbackService.createOrderItemsRollback(context);
throw new BusinessException(HttpStatus.CONFLICT, e.getMessage(), "5008");
Map<String, String> errorInfo = parseFeignExceptionMessage(e);
throw new BusinessException(HttpStatus.valueOf(e.status()), errorInfo);
}
}

Expand All @@ -106,7 +114,8 @@ public void checkAndUsePoint(OrderRollbackContext context, Order order, int poin
} catch (FeignException e) {
log.error("[User(Point)-Service FeignException]: UsePoint Request Error");
rollbackService.createOrderItemsRollback(context);
throw new BusinessException(HttpStatus.CONFLICT, e.getMessage(), "5008");
Map<String, String> errorInfo = parseFeignExceptionMessage(e);
throw new BusinessException(HttpStatus.valueOf(e.status()), errorInfo);
}
}

Expand All @@ -119,7 +128,8 @@ public void requestEasyPayment(OrderEasyPaymentRequestDto requestDto, Order orde
} catch (FeignException e) {
log.error("[Payment-Service FeignException]: EasyPay Request Error");
rollbackService.cancelOrderItemsRollback(order);
throw new BusinessException(ExceptionMessage.ORDER_PAYMENT_FAILED);
Map<String, String> errorInfo = parseFeignExceptionMessage(e);
throw new BusinessException(HttpStatus.valueOf(e.status()), errorInfo);
}
}

Expand All @@ -132,7 +142,8 @@ public void requestTossPayment(OrderTossPaymentRequestDto requestDto, Order orde
} catch (FeignException e) {
log.error("[Payment-Service FeignException]: TossPay Request Error");
rollbackService.cancelOrderItemsRollback(order);
throw new BusinessException(ExceptionMessage.ORDER_PAYMENT_FAILED);
Map<String, String> errorInfo = parseFeignExceptionMessage(e);
throw new BusinessException(HttpStatus.valueOf(e.status()), errorInfo);
}
}

Expand Down Expand Up @@ -172,4 +183,29 @@ public void processCancelOrder(Order order) {
// 주문 관련 롤백 요청
rollbackService.cancelOrderItemsRollback(order);
}

// FeignException -> status, message, code 추출
private Map<String, String> parseFeignExceptionMessage(FeignException e) {
String responseJson = e.contentUTF8();
Map<String, String> responseMap = new HashMap<>();

try {
// JSON을 파싱해서 message와 code 추출
Map<String, Object> parsedJson = objectMapper.readValue(responseJson,
new TypeReference<Map<String, Object>>() {
});

String message = (String) parsedJson.get("message");
String code = (String) parsedJson.get("code");

responseMap.put("message", message);
responseMap.put("code", code);

} catch (JsonProcessingException jsonException) {
log.error("Error parsing Feign response: {}", jsonException.getMessage());
throw new BusinessException(ExceptionMessage.FEIGN_CLIENT_PARSE_ERROR);
}

return responseMap;
}
}