From 8d8075c27396481fb1db3066b8cfd5a0e901fd34 Mon Sep 17 00:00:00 2001 From: jake Date: Thu, 17 Apr 2025 09:01:43 -0700 Subject: [PATCH 1/3] Raise exception for all 5xx errors with response body --- internetarchive/session.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/internetarchive/session.py b/internetarchive/session.py index fe85f57e..d3a0681a 100644 --- a/internetarchive/session.py +++ b/internetarchive/session.py @@ -552,6 +552,15 @@ def send(self, request, **kwargs) -> Response: except Exception: logger.error(e) raise e + + # Check for 5xx server errors and raise exception with response body + if 500 <= r.status_code < 600: + from requests.exceptions import HTTPError + raise HTTPError( + f"Server Error {r.status_code} occurred with response body: {r.text}", + response=r + ) + if self.protocol == 'http:': return r insecure_warnings = ['SNIMissingWarning', 'InsecurePlatformWarning'] From ac6f83bda6943a54d03906644a1619d2545004b7 Mon Sep 17 00:00:00 2001 From: jake Date: Thu, 17 Apr 2025 09:35:12 -0700 Subject: [PATCH 2/3] Monkey-patch Respons.json() to output response body if JSONDecdeError occurs --- internetarchive/session.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/internetarchive/session.py b/internetarchive/session.py index d3a0681a..2f856aa5 100644 --- a/internetarchive/session.py +++ b/internetarchive/session.py @@ -42,6 +42,7 @@ from requests import Response from requests.adapters import HTTPAdapter from requests.cookies import create_cookie +from requests.exceptions import JSONDecodeError from requests.utils import default_headers from urllib3 import Retry @@ -54,6 +55,21 @@ logger = logging.getLogger(__name__) +# Monkey-patch the .json() method to output the response body +# if a JSONDecodeError occurs. +original_json = Response.json +def json_with_body(self, *args, **kwargs): + try: + return original_json(self, *args, **kwargs) + except JSONDecodeError as e: + # Include the response body in the error message + body = self.text + msg = f"{e.msg} (Response body: {body})" + # Re-raise with the updated message + raise JSONDecodeError(msg, e.doc, e.pos) from None +Response.json = json_with_body # type: ignore[method-assign] + + class ArchiveSession(requests.sessions.Session): """The :class:`ArchiveSession ` object collects together useful functionality from `internetarchive` From 1f5828d230fda43d50523e4de0b651bb5fdf1c84 Mon Sep 17 00:00:00 2001 From: jake Date: Thu, 17 Apr 2025 09:37:32 -0700 Subject: [PATCH 3/3] updated 503 output test --- tests/test_item.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/test_item.py b/tests/test_item.py index 2324ebb4..9e0164eb 100644 --- a/tests/test_item.py +++ b/tests/test_item.py @@ -439,8 +439,6 @@ def test_upload_503(capsys, nasa_item): verbose=True) except Exception as exc: assert 'Please reduce your request rate' in str(exc) - out, err = capsys.readouterr() - assert 'warning: s3 is overloaded' in err def test_upload_file_keys(nasa_item):