Skip to content

Commit b3c0f4a

Browse files
committed
Added missing properties to error messages
1 parent 73e5dcf commit b3c0f4a

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

filenameprocessor/src/file_name_processor.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ def handle_record(record) -> dict:
4444
logger.error("Error obtaining file_key: %s", error)
4545
return {"statusCode": 500, "message": "Failed to download file key", "error": str(error)}
4646

47+
vaccine_type = "unknown"
48+
supplier = "unknown"
49+
4750
if "data-sources" in bucket_name:
4851

4952
# The lambda is unintentionally invoked when a file is moved into a different folder in the source bucket.
@@ -56,8 +59,6 @@ def handle_record(record) -> dict:
5659
# Set default values for file-specific variables
5760
message_id = "Message id was not created"
5861
created_at_formatted_string = "created_at_time not identified"
59-
vaccine_type = "unknown"
60-
supplier = "unknown"
6162

6263
try:
6364
# If the record contains a message_id, then the lambda has been invoked by a file already in the queue
@@ -66,9 +67,10 @@ def handle_record(record) -> dict:
6667
# Get message_id if the file is not new, else assign one
6768
message_id = record.get("message_id", str(uuid4()))
6869

70+
vaccine_type, supplier = validate_file_key(file_key)
71+
6972
created_at_formatted_string = get_created_at_formatted_string(bucket_name, file_key)
7073

71-
vaccine_type, supplier = validate_file_key(file_key)
7274
permissions = validate_vaccine_type_permissions(vaccine_type=vaccine_type, supplier=supplier)
7375
if not is_existing_file:
7476
ensure_file_is_not_a_duplicate(file_key, created_at_formatted_string)
@@ -145,12 +147,15 @@ def handle_record(record) -> dict:
145147
except Exception as error: # pylint: disable=broad-except
146148
logger.error("Error uploading to cache for file '%s': %s", file_key, error)
147149
message = "Failed to upload file content to cache"
148-
return {"statusCode": 500, "message": message, "file_key": file_key, "error": str(error)}
150+
return {"statusCode": 500, "message": message, "file_key": file_key, "error": str(error),
151+
"vaccine_type": vaccine_type, "supplier": supplier}
149152

150153
else:
151154
logger.error("Unable to process file %s due to unexpected bucket name %s", file_key, bucket_name)
152155
message = f"Failed to process file due to unexpected bucket name {bucket_name}"
153-
return {"statusCode": 500, "message": message, "file_key": file_key}
156+
157+
return {"statusCode": 500, "message": message, "file_key": file_key,
158+
"vaccine_type": vaccine_type, "supplier": supplier}
154159

155160

156161
def lambda_handler(event: dict, context) -> None: # pylint: disable=unused-argument
@@ -161,3 +166,23 @@ def lambda_handler(event: dict, context) -> None: # pylint: disable=unused-argu
161166
handle_record(record)
162167

163168
logger.info("Filename processor lambda task completed")
169+
170+
171+
if __name__ == "__main__":
172+
173+
event = {
174+
"Records": [
175+
{
176+
"s3": {
177+
"bucket": {
178+
"name": "immunisation-batch-internal-dev-data-sources"
179+
},
180+
"object": {
181+
"key": "FLU_Vaccinations_v5_YGM41_20000101T00000001.csv"
182+
}
183+
}
184+
}
185+
]
186+
}
187+
print(event)
188+
print(lambda_handler(event=event, context={}))

filenameprocessor/src/logging_decorator.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from datetime import datetime
77
from functools import wraps
88
from clients import firehose_client, logger
9+
from file_key_validation import validate_file_key
10+
911

1012
STREAM_NAME = os.getenv("SPLUNK_FIREHOSE_NAME", "immunisation-fhir-api-internal-dev-splunk-firehose")
1113

@@ -51,7 +53,11 @@ def wrapper(*args, **kwargs):
5153
return result
5254

5355
except Exception as e:
54-
additional_log_data = {"statusCode": 500, "error": str(e)}
56+
file_key = args[0]["s3"]["object"]["key"]
57+
vaccine_type, supplier = validate_file_key(file_key)
58+
59+
additional_log_data = {"statusCode": 500, "error": str(e), "vaccine_type": vaccine_type,
60+
"supplier": supplier}
5561
generate_and_send_logs(start_time, base_log_data, additional_log_data, is_error_log=True)
5662
raise
5763

0 commit comments

Comments
 (0)