Skip to content

Commit dfc0d9e

Browse files
committed
validation error
1 parent 9bd9f71 commit dfc0d9e

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

recordprocessor/src/batch_processor.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,8 @@ def process_csv_to_fhir(incoming_message_body: dict) -> int:
2525
# and encoder to the incoming message body
2626
incoming_message_body["encoder"] = encoder
2727
interim_message_body = file_level_validation(incoming_message_body=incoming_message_body)
28-
except InvalidEncoding as error:
29-
logger.warning("Invalid Encoding detected in process_csv_to_fhir: %s", error)
30-
# retry with cp1252 encoding
31-
encoder = "cp1252"
32-
try:
33-
incoming_message_body["encoder"] = encoder
34-
interim_message_body = file_level_validation(incoming_message_body=incoming_message_body)
35-
except Exception as error:
36-
logger.error(f"Error in file_level_validation with {encoder} encoding: %s", error)
37-
return 0
38-
except (InvalidHeaders, NoOperationPermissions, Exception): # pylint: disable=broad-exception-caught
28+
except (InvalidHeaders, NoOperationPermissions, Exception) as e: # pylint: disable=broad-exception-caught
29+
logger.error(f"File level validation failed: {e}")
3930
# If the file is invalid, processing should cease immediately
4031
return 0
4132

recordprocessor/src/file_level_validation.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Functions for completing file-level validation
33
(validating headers and ensuring that the supplier has permission to perform at least one of the requested operations)
44
"""
5+
from curses import error
56
from clients import logger, s3_client
67
from make_and_upload_ack_file import make_and_upload_ack_file
78
from utils_for_recordprocessor import get_csv_content_dict_reader, invoke_filename_lambda
@@ -79,9 +80,17 @@ def file_level_validation(incoming_message_body: dict) -> dict:
7980
encoder = incoming_message_body.get("encoder", "utf-8")
8081

8182
# Fetch the data
82-
csv_reader = get_csv_content_dict_reader(file_key, encoder=encoder)
83-
84-
validate_content_headers(csv_reader)
83+
try:
84+
csv_reader = get_csv_content_dict_reader(file_key, encoder=encoder)
85+
validate_content_headers(csv_reader)
86+
except Exception as e:
87+
if hasattr(e, 'reason') and e.reason == "invalid continuation byte" and encoder == "utf-8":
88+
logger.warning("Invalid Encoding detected: %s", e)
89+
# retry with cp1252 encoding
90+
csv_reader = get_csv_content_dict_reader(file_key, encoder="cp1252")
91+
validate_content_headers(csv_reader)
92+
else:
93+
raise
8594

8695
# Validate has permission to perform at least one of the requested actions
8796
allowed_operations_set = get_permitted_operations(supplier, vaccine, permission)

recordprocessor/tests/test_recordprocessor_edge_cases.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ def test_process_large_file_with_utf8(self):
104104
def test_process_cp1252_small_file(self):
105105
""" Test processing a small file with cp1252 encoding """
106106
data = self.create_test_data_from_file("test-batch-data-cp1252.csv")
107+
data = self.insert_cp1252_at_end(data, b'D\xe9cembre', 2)
107108
data = [line if line.endswith(b"\n") else line + b"\n" for line in data]
108109
n_rows = len(data) - 1 # Exclude header
109110

@@ -124,7 +125,7 @@ def test_process_cp1252_small_file(self):
124125
self.assertEqual(self.mock_send_to_kinesis.call_count, n_rows)
125126
self.mock_logger_warning.assert_called()
126127
warning_call_args = self.mock_logger_warning.call_args[0][0]
127-
self.assertTrue(warning_call_args.startswith("Invalid Encoding detected in process_csv_to_fhir"))
128+
self.assertTrue(warning_call_args.startswith("Invalid Encoding detected"))
128129

129130
def test_process_utf8_small_file(self):
130131
""" Test processing a small file with utf-8 encoding """

0 commit comments

Comments
 (0)