diff --git a/commerce_coordinator/apps/commercetools/clients.py b/commerce_coordinator/apps/commercetools/clients.py index 877871b9..159e3b04 100644 --- a/commerce_coordinator/apps/commercetools/clients.py +++ b/commerce_coordinator/apps/commercetools/clients.py @@ -40,7 +40,7 @@ from commerce_coordinator.apps.commercetools.catalog_info.constants import DEFAULT_ORDER_EXPANSION, EdXFieldNames from commerce_coordinator.apps.commercetools.catalog_info.foundational_types import TwoUCustomTypes -from commerce_coordinator.apps.commercetools.utils import translate_stripe_refund_status_to_transaction_status +from commerce_coordinator.apps.commercetools.utils import translate_stripe_refund_status_to_transaction_status, handle_commercetools_error from commerce_coordinator.apps.core.constants import ORDER_HISTORY_PER_SYSTEM_REQ_LIMIT logger = logging.getLogger(__name__) @@ -362,9 +362,7 @@ def create_return_for_order(self, order_id: str, order_version: int, order_line_ ) return returned_order except CommercetoolsError as err: - logger.error(f"[CommercetoolsError] Unable to create return for " - f"order {order_id} with error correlation id {err.correlation_id} " - f"and error/s: {err.errors}") + handle_commercetools_error(err, f"Unable to create return for order {order_id}") raise err def update_return_payment_state_after_successful_refund(self, order_id: str, @@ -398,9 +396,7 @@ def update_return_payment_state_after_successful_refund(self, order_id: str, ) return updated_order except CommercetoolsError as err: - logger.error(f"[CommercetoolsError] Unable to update ReturnPaymentState " - f"of order {order_id} with error correlation id {err.correlation_id} " - f"and error/s: {err.errors}") + handle_commercetools_error(err, f"Unable to update ReturnPaymentState of order {order_id}") raise OpenEdxFilterException(str(err)) from err def create_return_payment_transaction( @@ -445,9 +441,7 @@ def create_return_payment_transaction( return returned_payment except CommercetoolsError as err: - logger.error(f"[CommercetoolsError] Unable to create refund payment transaction for " - f"payment {payment_id} and stripe refund {stripe_refund.id} with " - f"error correlation id {err.correlation_id} and error/s: {err.errors}") + handle_commercetools_error(err, f"Unable to create refund payment transaction for payment {payment_id}") raise err def update_line_item_transition_state_on_fulfillment(self, order_id: str, order_version: int, @@ -494,7 +488,5 @@ def update_line_item_transition_state_on_fulfillment(self, order_id: str, order_ return self.get_order_by_id(order_id) except CommercetoolsError as err: # Logs & ignores version conflict errors due to duplicate Commercetools messages - logger.error(f"[CommercetoolsError] Unable to update LineItemState " - f"of order {order_id} with error correlation id {err.correlation_id} " - f"and error/s: {err.errors}") + handle_commercetools_error(err, f"Unable to update LineItemState of order {order_id}") return None diff --git a/commerce_coordinator/apps/commercetools/utils.py b/commerce_coordinator/apps/commercetools/utils.py index 5d387be6..e7f30535 100644 --- a/commerce_coordinator/apps/commercetools/utils.py +++ b/commerce_coordinator/apps/commercetools/utils.py @@ -8,7 +8,9 @@ import requests from braze.client import BrazeClient +from commercetools import CommercetoolsError from commercetools.platform.models import Customer, LineItem, Order, Payment, TransactionState, TransactionType + from django.conf import settings from django.urls import reverse @@ -31,6 +33,10 @@ def get_braze_client(): app_id='', ) +def handle_commercetools_error(err: CommercetoolsError, context: str) -> None: + error_details = [f"Code: {e.code}, Message: {e.message}" for e in err.errors] + error_message = f"[CommercetoolsError] {context} - Correlation ID: {err.correlation_id}, Details: {error_details}" + logger.error(error_message) def send_order_confirmation_email( lms_user_id, lms_user_email, canvas_entry_properties