Skip to content

Commit 18ab5c0

Browse files
committed
Only use string error values in _raise_for_status
Only set the Space-Track error message from the JSON 'error' value when it is a string; anything else falls back to showing the raw response body. A truthy non-string value previously raised TypeError from the message concatenation instead of the intended HTTPStatusError, so callers catching HTTPStatusError missed the failure. No such response has been observed from Space-Track; this is defensive hardening of the error reporting path.
1 parent 168de31 commit 18ab5c0

3 files changed

Lines changed: 17 additions & 1 deletion

File tree

newsfragments/172.fixed.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Error responses whose JSON ``error`` value is not a string no longer raise :class:`TypeError` instead of ``httpx2.HTTPStatusError``.

src/spacetrack/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,7 @@ def _raise_for_status(response):
11571157

11581158
try:
11591159
json = response.json()
1160-
if isinstance(json, Mapping):
1160+
if isinstance(json, Mapping) and isinstance(json["error"], str):
11611161
spacetrack_error_msg = json["error"]
11621162
except (ValueError, KeyError, httpx2.ResponseNotRead):
11631163
pass

tests/test_spacetrack.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,21 @@ def test_raise_for_status(httpx2_mock):
467467
assert "Space-Track" not in str(exc.value)
468468

469469

470+
def test_raise_for_status_non_string_error(httpx2_mock):
471+
httpx2_mock.add_response(
472+
method="GET",
473+
url="http://example.com/1",
474+
status_code=400,
475+
json={"error": 12345},
476+
)
477+
478+
response = httpx2.get("http://example.com/1")
479+
480+
with pytest.raises(httpx2.HTTPStatusError) as exc:
481+
_raise_for_status(response)
482+
assert '{"error":12345}' in str(exc.value)
483+
484+
470485
def test_repr(httpx2_mock):
471486
with SpaceTrackClient("hello@example.com", "mypassword") as client:
472487
assert repr(client) == "SpaceTrackClient<identity='hello@example.com'>"

0 commit comments

Comments
 (0)