Skip to content

Commit bb839d7

Browse files
committed
Add HTTP status context to XML parsing error messages
Enhanced ResponseParserError messages to include HTTP status codes and messages when XML parsing fails, providing better debugging context for users while maintaining full backward compatibility. Changes: - Modified QueryParser._do_error_parse() to append HTTP status context - Modified RestXMLParser._parse_error_from_body() to append HTTP status context - Enhanced error messages format: "original error (HTTP 413: Content Too Large)" - Preserves original ResponseParserError exception type for compatibility - Added comprehensive tests for both parser classes - Tests handle Python version differences in HTTP status messages
1 parent 6d3a89f commit bb839d7

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

.python-version

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
3.9.20
2+
3.10.15
3+
3.11.10
4+
3.12.7
5+
3.13.9

botocore/parsers.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -595,11 +595,13 @@ def _do_error_parse(self, response, shape):
595595
try:
596596
root = self._parse_xml_string_to_dom(xml_contents)
597597
except ResponseParserError as e:
598-
status_message = http.client.responses.get(
599-
response['status_code'], ''
600-
)
601-
if status_message and hasattr(e, 'add_note'):
602-
e.add_note(f"HTTP {response['status_code']}: {status_message}")
598+
status_code = response.get('status_code')
599+
if status_code and status_code in http.client.responses:
600+
status_message = http.client.responses[status_code]
601+
error_msg_with_status = (
602+
f"{str(e)} (HTTP {status_code}: {status_message})"
603+
)
604+
raise ResponseParserError(error_msg_with_status)
603605
raise
604606
parsed = self._build_name_to_xml_node(root)
605607
self._replace_nodes(parsed)
@@ -1458,11 +1460,12 @@ def _parse_error_from_body(self, response):
14581460
try:
14591461
root = self._parse_xml_string_to_dom(xml_contents)
14601462
except ResponseParserError as e:
1461-
status_message = http.client.responses.get(
1462-
response['status_code'], ''
1463-
)
1464-
if status_message and hasattr(e, 'add_note'):
1465-
e.add_note(f"HTTP {response['status_code']}: {status_message}")
1463+
status_code = response.get('status_code')
1464+
if status_code and status_code in http.client.responses:
1465+
status_message = http.client.responses[status_code]
1466+
raise ResponseParserError(
1467+
f"{str(e)} (HTTP {status_code}: {status_message})"
1468+
)
14661469
raise
14671470
parsed = self._build_name_to_xml_node(root)
14681471
self._replace_nodes(parsed)

tests/unit/test_parsers.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,10 +1637,13 @@ def test_query_parser_empty_body_4xx_error_with_notes(self):
16371637
parser._do_error_parse(response, None)
16381638

16391639
exception = cm.exception
1640-
1641-
self.assertTrue(hasattr(exception, '__notes__'))
1642-
self.assertEqual(len(exception.__notes__), 1)
1643-
self.assertIn("HTTP 413:", exception.__notes__[0])
1640+
self.assertIn("HTTP 413:", str(exception))
1641+
error_msg = str(exception)
1642+
self.assertTrue(
1643+
"Request Entity Too Large" in error_msg
1644+
or "Content Too Large" in error_msg,
1645+
f"Expected HTTP 413 message not found in: {error_msg}",
1646+
)
16441647

16451648
def test_parse_error_from_body_empty_body_4xx_error_with_notes(self):
16461649
parser = parsers.RestXMLParser()
@@ -1658,10 +1661,13 @@ def test_parse_error_from_body_empty_body_4xx_error_with_notes(self):
16581661
parser._parse_error_from_body(response)
16591662

16601663
exception = cm.exception
1661-
1662-
self.assertTrue(hasattr(exception, '__notes__'))
1663-
self.assertEqual(len(exception.__notes__), 1)
1664-
self.assertIn("HTTP 413:", exception.__notes__[0])
1664+
self.assertIn("HTTP 413:", str(exception))
1665+
error_msg = str(exception)
1666+
self.assertTrue(
1667+
"Request Entity Too Large" in error_msg
1668+
or "Content Too Large" in error_msg,
1669+
f"Expected HTTP 413 message not found in: {error_msg}",
1670+
)
16651671

16661672

16671673
def _generic_test_bodies():

0 commit comments

Comments
 (0)