diff --git a/containers/message-refiner/app/refine.py b/containers/message-refiner/app/refine.py index abbfd9ee9e..17d1f7e045 100644 --- a/containers/message-refiner/app/refine.py +++ b/containers/message-refiner/app/refine.py @@ -1,3 +1,4 @@ +import logging from typing import Dict from typing import List from typing import Optional @@ -5,9 +6,12 @@ from lxml import etree -# read json that contains details for refining and is the base of what drives `refine` from app.utils import read_json_from_assets +log = logging.getLogger(__name__).error + + +# read json that contains details for refining and is the base of what drives `refine` REFINER_DETAILS = read_json_from_assets("refiner_details.json") # extract section LOINC codes from the REFINER_DETAILS dictionary @@ -34,8 +38,9 @@ def validate_message(raw_message: str) -> tuple[bytes | None, str]: validated_message = etree.fromstring(raw_message) return (validated_message, error_message) except etree.XMLSyntaxError as error: - error_message = f"XMLSyntaxError: {error}" - return (None, str(error_message)) + error_message = "Invalid XML format." + log(f"XMLSyntaxError: {error}") + return (None, error_message) def validate_sections_to_include(sections_to_include: str | None) -> tuple[list, str]: @@ -57,7 +62,8 @@ def validate_sections_to_include(sections_to_include: str | None) -> tuple[list, sections = sections_to_include.split(",") for section in sections: if section not in SECTION_LOINCS: - error_message = f"{section} is invalid. Please provide a valid section." + error_message = "Invalid section provided." + log(f"Invalid section: {section}") return (section_loincs, error_message) section_loincs.append(section) diff --git a/containers/message-refiner/tests/test_refine.py b/containers/message-refiner/tests/test_refine.py index 052f26a31e..a2a48d4fc3 100644 --- a/containers/message-refiner/tests/test_refine.py +++ b/containers/message-refiner/tests/test_refine.py @@ -116,7 +116,7 @@ def normalize_xml(xml_string): # Test case: invalid sections_to_include ( "blah blah blah", - ([], "blah blah blah is invalid. Please provide a valid section."), + ([], "Invalid section provided."), ), ], ) @@ -151,7 +151,7 @@ def test_validate_message(): raw_message = "this is not a valid XML" actual_response, error_message = validate_message(raw_message) assert actual_response is None - assert "XMLSyntaxError" in error_message + assert "Invalid XML format." in error_message def test_generate_combined_xpath(): diff --git a/containers/message-refiner/tests/test_refiner.py b/containers/message-refiner/tests/test_refiner.py index 98b6d0fd1d..7e141cae58 100644 --- a/containers/message-refiner/tests/test_refiner.py +++ b/containers/message-refiner/tests/test_refiner.py @@ -128,7 +128,7 @@ def test_ecr_refiner(): assert actual_flattened == expected_flattened # Test case: sections_to_include is invalid - expected_response = "blah blah blah is invalid. Please provide a valid section." + expected_response = "Invalid section provided." content = test_eICR_xml sections_to_include = "blah blah blah" endpoint = f"/ecr/?sections_to_include={sections_to_include}" @@ -142,7 +142,7 @@ def test_ecr_refiner(): endpoint = "/ecr/" actual_response = client.post(endpoint, content=content) assert actual_response.status_code == 400 - assert "XMLSyntaxError" in actual_response.content.decode() + assert "Invalid XML format." in actual_response.content.decode() @pytest.mark.asyncio