diff --git a/internetarchive/session.py b/internetarchive/session.py index fe85f57e..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` @@ -552,6 +568,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'] 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):