Skip to content

Commit

Permalink
Update error logging to ensure the errored field is included in the logs
Browse files Browse the repository at this point in the history
Update docstring wording
  • Loading branch information
DeanElliott96 committed Feb 14, 2025
1 parent 29e7f19 commit e916fa1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
10 changes: 4 additions & 6 deletions datahub/company_activity/tasks/ingest_stova_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,10 @@ def _process_record(self, record: dict) -> None:
}

try:
StovaEvent.objects.create(**values)
except IntegrityError as error:
logger.error(
f'Error processing Stova event record, stova_event_id: {stova_event_id}. '
f'Error: {error}',
)
stova_event = StovaEvent(**values)
# Raises Validation errors if there are any with the field/s which errored
stova_event.full_clean()
stova_event.save()
except ValidationError as error:
logger.error(
'Got unexpected value for a field when processing Stova event record, '
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def test_stova_event_fields_with_duplicate_attendee_ids_in_json(
self, caplog, test_base_stova_event,
):
"""
Test records which have duplicate IDs in their JSON do not raise errors and are logged.
Tests records which have duplicate IDs should have errors logged.
"""
s3_processor_mock = mock.Mock()
task = StovaEventIngestionTask('dummy-prefix', s3_processor_mock)
Expand All @@ -223,13 +223,18 @@ def test_stova_event_fields_with_duplicate_attendee_ids_in_json(
task._process_record(data)
with caplog.at_level(logging.ERROR):
task._process_record(data)
assert f'Error processing Stova event record, stova_event_id: {999999}' in caplog.text
assert (
'Got unexpected value for a field when processing Stova event record, '
'stova_event_id: 999999. Error: {\'stova_event_id\': [\'Stova event with this '
'Stova event id already exists.\']}' in caplog.text
)

@pytest.mark.django_db
def test_stova_event_ingestion_handles_unexpected_fields(self, caplog, test_base_stova_event):
"""
Test that if they rows from stova contain data in an unexpected data type these are handled
and logged.
Also assert the errored field is displayed.
"""
s3_processor_mock = mock.Mock()
task = StovaEventIngestionTask('dummy-prefix', s3_processor_mock)
Expand All @@ -246,3 +251,5 @@ def test_stova_event_ingestion_handles_unexpected_fields(self, caplog, test_base
'Got unexpected value for a field when processing Stova event record, '
f'stova_event_id: {stova_event_id}'
) in caplog.text

assert ('approval_required') in caplog.text

0 comments on commit e916fa1

Please sign in to comment.