Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions internetarchive/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 <internetarchive.ArchiveSession>`
object collects together useful functionality from `internetarchive`
Expand Down Expand Up @@ -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']
Expand Down
2 changes: 0 additions & 2 deletions tests/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down