Skip to content

Commit febc1ff

Browse files
authored
Merge pull request #172 from python-astrodynamics/feature/error-message
Handle non-string error values in error responses
2 parents 45a49b4 + 18ab5c0 commit febc1ff

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
@@ -1226,7 +1226,7 @@ def _raise_for_status(response):
12261226

12271227
try:
12281228
json = response.json()
1229-
if isinstance(json, Mapping):
1229+
if isinstance(json, Mapping) and isinstance(json["error"], str):
12301230
spacetrack_error_msg = json["error"]
12311231
except (ValueError, KeyError, httpx2.ResponseNotRead):
12321232
pass

‎tests/test_spacetrack.py‎

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

726726

727+
def test_raise_for_status_non_string_error(httpx2_mock):
728+
httpx2_mock.add_response(
729+
method="GET",
730+
url="http://example.com/1",
731+
status_code=400,
732+
json={"error": 12345},
733+
)
734+
735+
response = httpx2.get("http://example.com/1")
736+
737+
with pytest.raises(httpx2.HTTPStatusError) as exc:
738+
_raise_for_status(response)
739+
assert '{"error":12345}' in str(exc.value)
740+
741+
727742
def test_repr(httpx2_mock):
728743
with SpaceTrackClient("hello@example.com", "mypassword") as client:
729744
assert repr(client) == "SpaceTrackClient<identity='hello@example.com'>"

0 commit comments

Comments
 (0)