Skip to content

Commit

Permalink
fix information expsure through error message (#2993)
Browse files Browse the repository at this point in the history
* fix information expsure through error message

* fix tests

* fix tests

* fix tests
  • Loading branch information
gordonfarrell authored Dec 9, 2024
1 parent 71feb4c commit 4773df9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
14 changes: 10 additions & 4 deletions containers/message-refiner/app/refine.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import logging
from typing import Dict
from typing import List
from typing import Optional
from typing import Union

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
Expand All @@ -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]:
Expand All @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions containers/message-refiner/tests/test_refine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."),
),
],
)
Expand Down Expand Up @@ -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():
Expand Down
4 changes: 2 additions & 2 deletions containers/message-refiner/tests/test_refiner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -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
Expand Down

0 comments on commit 4773df9

Please sign in to comment.