Skip to content

Commit

Permalink
fix: make HttpException handle response with no body properly
Browse files Browse the repository at this point in the history
A call to e.g. HEAD returns an empty body, which makes the `.json()`
throw a decode exception.

This commit fixes it by always returning a dict in the exceptions's
`json` and guarding against None body.
  • Loading branch information
derlin committed Oct 27, 2024
1 parent 838d64c commit ea4d4b7
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
4 changes: 2 additions & 2 deletions mantelo/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class HttpException(Exception):

status_code: int
"""The HTTP status code."""
json: str
json: dict
"""The JSON response from the server."""
url: str
"""The URL that was requested."""
Expand All @@ -38,6 +38,6 @@ def from_slumber_exception(cls, ex: SlumberHttpBaseException):
return cls(
url=ex.response.request.url,
status_code=ex.response.status_code,
json=ex.response.json(),
json=ex.response.json() if ex.response.content else {},
response=ex.response,
)
3 changes: 2 additions & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def test_switch_realm(openid_connection_admin):
(403, lambda c: c.clients.get()),
(400, lambda c: c.users.post({"foo": "bar"})),
(400, lambda c: c.users.post({})),
(405, lambda c: c.groups.head()),
(405, lambda c: c.users.put({})),
(404, lambda c: c.users("not-exist").get()),
(
Expand All @@ -140,7 +141,7 @@ def test_exceptions(openid_connection_password, status, op):
ex = excinfo.value
assert ex.status_code == status
assert ex.url.startswith(constants.TEST_SERVER_URL)
assert ex.json and isinstance(ex.json, dict)
assert isinstance(ex.json, dict)
assert isinstance(ex.response, requests.Response)


Expand Down

0 comments on commit ea4d4b7

Please sign in to comment.