From 9483664c998a0ca485933723897b16775f0eb538 Mon Sep 17 00:00:00 2001 From: jawad khan Date: Thu, 26 Oct 2023 14:10:10 +0500 Subject: [PATCH] fix: Added logs for ios refund exception (#4040) * fix: Added logs for ios refund exception --- ecommerce/extensions/iap/api/v1/constants.py | 1 + .../extensions/iap/api/v1/tests/test_views.py | 30 +++++++++++++++++-- ecommerce/extensions/iap/api/v1/views.py | 6 ++-- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/ecommerce/extensions/iap/api/v1/constants.py b/ecommerce/extensions/iap/api/v1/constants.py index 4ba77782b25..7bfcd9ed8e8 100644 --- a/ecommerce/extensions/iap/api/v1/constants.py +++ b/ecommerce/extensions/iap/api/v1/constants.py @@ -6,6 +6,7 @@ ERROR_ALREADY_PURCHASED = "You have already purchased these products" ERROR_BASKET_NOT_FOUND = "Basket [{}] not found." ERROR_BASKET_ID_NOT_PROVIDED = "Basket id is not provided" +ERROR_DURING_IOS_REFUND_EXECUTION = "Could not execute IOS refund." ERROR_DURING_ORDER_CREATION = "An error occurred during order creation." ERROR_DURING_PAYMENT_HANDLING = "An error occurred during payment handling." ERROR_ORDER_NOT_FOUND_FOR_REFUND = "Could not find any order to refund for [%s] by processor [%s]" diff --git a/ecommerce/extensions/iap/api/v1/tests/test_views.py b/ecommerce/extensions/iap/api/v1/tests/test_views.py index b1ddebea2ed..16ce322faa0 100644 --- a/ecommerce/extensions/iap/api/v1/tests/test_views.py +++ b/ecommerce/extensions/iap/api/v1/tests/test_views.py @@ -31,6 +31,7 @@ ERROR_ALREADY_PURCHASED, ERROR_BASKET_ID_NOT_PROVIDED, ERROR_BASKET_NOT_FOUND, + ERROR_DURING_IOS_REFUND_EXECUTION, ERROR_DURING_ORDER_CREATION, ERROR_DURING_PAYMENT_HANDLING, ERROR_DURING_POST_ORDER_OP, @@ -923,7 +924,7 @@ class IOSRefundTests(BaseRefundTests): processor_name = IOSIAP.NAME def assert_error_response(self, response): - """ Assert the response has HTTP status 200 and no data. """ + """ Assert the response has HTTP status 500 and no data. """ self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) def check_record_not_found_log(self, logger, msg_t): @@ -992,7 +993,7 @@ def test_valid_orders(self): def test_non_refund_notification(self): """ - View should create a refund if an order/line are found eligible for refund. + View should ignore non refund notifications """ with mock.patch.object(asn2, 'parse') as mock_ios_response_parse,\ @@ -1015,3 +1016,28 @@ def test_non_refund_notification(self): IGNORE_NON_REFUND_NOTIFICATION_FROM_APPLE ) ) + + def test_unknown_error_in_parsing_or_refund(self): + """ + If there is any other error in parsing or executing refund, View should log it and return 500. + """ + + with mock.patch.object(asn2, 'parse') as mock_ios_response_parse, \ + LogCapture(self.logger_name) as logger: + + mock_ios_response_parse.side_effect = Exception("Test Exception") + + response = self.client.post(self.path) + self.assert_error_response(response) + logger.check( + ( + self.logger_name, + 'ERROR', + ERROR_DURING_IOS_REFUND_EXECUTION + ), + ( + self.logger_name, + 'ERROR', + "Test Exception" + ), + ) diff --git a/ecommerce/extensions/iap/api/v1/views.py b/ecommerce/extensions/iap/api/v1/views.py index c80d4605235..647b736d702 100644 --- a/ecommerce/extensions/iap/api/v1/views.py +++ b/ecommerce/extensions/iap/api/v1/views.py @@ -39,6 +39,7 @@ ERROR_ALREADY_PURCHASED, ERROR_BASKET_ID_NOT_PROVIDED, ERROR_BASKET_NOT_FOUND, + ERROR_DURING_IOS_REFUND_EXECUTION, ERROR_DURING_ORDER_CREATION, ERROR_DURING_PAYMENT_HANDLING, ERROR_DURING_POST_ORDER_OP, @@ -359,8 +360,9 @@ def post(self, request): logger.info(IGNORE_NON_REFUND_NOTIFICATION_FROM_APPLE) return Response(status=status.HTTP_200_OK) - except Exception: # pylint: disable=broad-except - pass + except Exception as e: # pylint: disable=broad-except + logger.error(ERROR_DURING_IOS_REFUND_EXECUTION) + logger.error(e) status_code = status.HTTP_200_OK if is_refunded else status.HTTP_500_INTERNAL_SERVER_ERROR return Response(status=status_code)