From b7e0c4b231ccbd75adedd6dd0ec6a95fcd535e09 Mon Sep 17 00:00:00 2001 From: Cristhian Garcia Date: Tue, 15 Oct 2024 15:26:37 -0500 Subject: [PATCH] fix: catch json errors on empty responses from ralph (#461) * fix: catch json errors on empty responses from ralph * fix: move JSONDecodeError catch statement to xapi lrs * fix: move JSONDecodeError catch statement to xapi lrs * chore: remove unnecessary condition * refactor: define response outside try-except * test: add tests for jsondecode error * chore: update duplicated message * chore: quality changes * chore: quality changes --- .../backends/tests/test_events_router.py | 17 +++++++++++++++++ event_routing_backends/utils/xapi_lrs_client.py | 12 +++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/event_routing_backends/backends/tests/test_events_router.py b/event_routing_backends/backends/tests/test_events_router.py index 25a016b2..511400ff 100644 --- a/event_routing_backends/backends/tests/test_events_router.py +++ b/event_routing_backends/backends/tests/test_events_router.py @@ -4,6 +4,7 @@ import datetime import json from copy import copy +from json import JSONDecodeError from unittest.mock import MagicMock, call, patch, sentinel import ddt @@ -273,6 +274,22 @@ def test_duplicate_xapi_event_id(self, mocked_logger): mocked_logger.info.mock_calls ) + @patch('event_routing_backends.utils.xapi_lrs_client.logger') + def test_duplicate_xapi_event_id_json(self, mocked_logger): + """ + Test that when we receive a 204 response (and the LRSClient fails to parse to JSON + the response) when bulk inserting XAPI statements it may indicates all events are already stored. + """ + client = LrsClient({}) + client.lrs_client = MagicMock() + client.lrs_client.save_statements.side_effect = JSONDecodeError(msg="msg", doc="...", pos=0) + + client.bulk_send(statement_data=[]) + self.assertIn( + call('JSON Decode Error, this may indicate that all sent events are already stored: []'), + mocked_logger.warning.mock_calls + ) + @override_settings( EVENT_ROUTING_BACKEND_BATCHING_ENABLED=True, EVENT_ROUTING_BACKEND_BATCH_SIZE=2 diff --git a/event_routing_backends/utils/xapi_lrs_client.py b/event_routing_backends/utils/xapi_lrs_client.py index 4e9f1216..45f2e2b0 100644 --- a/event_routing_backends/utils/xapi_lrs_client.py +++ b/event_routing_backends/utils/xapi_lrs_client.py @@ -1,6 +1,7 @@ """ An LRS client for xAPI stores. """ +from json.decoder import JSONDecodeError from logging import getLogger from tincan.remote_lrs import RemoteLRS @@ -71,8 +72,17 @@ def bulk_send(self, statement_data): requests.Response object """ logger.debug('Sending {} xAPI statements to {}'.format(len(statement_data), self.URL)) + response = None - response = self.lrs_client.save_statements(statement_data) + try: + response = self.lrs_client.save_statements(statement_data) + except JSONDecodeError: + logger.warning( + f"JSON Decode Error, this may indicate that all sent events are already stored: {statement_data}" + ) + + if not response: + return if not response.success: if response.response.code == 409: