Skip to content

Commit 48b3ab0

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 3da8e84 commit 48b3ab0

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 :class:`httpx2.HTTPStatusError`; the raw response body is included in the message instead.

src/spacetrack/base.py

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

11281128
try:
11291129
json = response.json()
1130-
if isinstance(json, Mapping):
1130+
if isinstance(json, Mapping) and isinstance(json["error"], str):
11311131
spacetrack_error_msg = json["error"]
11321132
except (ValueError, KeyError, httpx2.ResponseNotRead):
11331133
pass

tests/test_spacetrack.py

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

442442

443+
def test_raise_for_status_non_string_error(httpx2_mock):
444+
httpx2_mock.add_response(
445+
method="GET",
446+
url="http://example.com/1",
447+
status_code=400,
448+
json={"error": 12345},
449+
)
450+
451+
response = httpx2.get("http://example.com/1")
452+
453+
with pytest.raises(httpx2.HTTPStatusError) as exc:
454+
_raise_for_status(response)
455+
assert '{"error":12345}' in str(exc.value)
456+
457+
443458
def test_repr(httpx2_mock):
444459
with SpaceTrackClient("hello@example.com", "mypassword") as client:
445460
assert repr(client) == "SpaceTrackClient<identity='hello@example.com'>"

0 commit comments

Comments
 (0)