Skip to content

Commit 3551f40

Browse files
committed
Do not assume valid JSON body for bad responses
1 parent cbc7ed4 commit 3551f40

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

changelog.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
# Changes in 1.16
1+
# Changes in 1.17
22

33
## Breaking changes
44

55
## New features
66

77
## Bug fixes
88

9+
* Fix reporting error based on http responses from the Aura-API with an invalid JSON body. Earlier the client would report JSONDecodeError instead of showing the actual issue.
10+
911
## Improvements
1012

1113
## Other changes

graphdatascience/session/aura_api.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,17 @@ def _check_resp(self, resp: requests.Response) -> None:
351351

352352
def _check_status_code(self, resp: requests.Response) -> None:
353353
if resp.status_code >= 400:
354+
message = ""
355+
try:
356+
message = resp.json()
357+
except requests.JSONDecodeError:
358+
try:
359+
message = resp.text
360+
except Exception:
361+
message = f"Not parsable body `{resp.raw.data!r}`"
362+
354363
raise AuraApiError(
355-
f"Request for {resp.url} failed with status code {resp.status_code} - {resp.reason}: {resp.json()}",
364+
f"Request for {resp.url} failed with status code {resp.status_code} - {resp.reason}: `{message}`",
356365
status_code=resp.status_code,
357366
)
358367

graphdatascience/tests/unit/test_aura_api.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,43 @@ def test_list_missing_instance(requests_mock: Mocker) -> None:
948948
assert api.list_instance("id0") is None
949949

950950

951+
def test_list_instance_unknown_error(requests_mock: Mocker) -> None:
952+
api = AuraApi("", "", project_id="some-tenant")
953+
954+
mock_auth_token(requests_mock)
955+
956+
requests_mock.get(
957+
"https://api.neo4j.io/v1/instances/id0",
958+
status_code=500,
959+
reason="Not Found",
960+
text="my text",
961+
)
962+
963+
with pytest.raises(
964+
AuraApiError,
965+
match="Request for https://api.neo4j.io/v1/instances/id0 failed with status code 500 - Not Found: `my text`'",
966+
):
967+
api.list_instance("id0")
968+
969+
970+
def test_list_instance_unknown_error_empty_body(requests_mock: Mocker) -> None:
971+
api = AuraApi("", "", project_id="some-tenant")
972+
973+
mock_auth_token(requests_mock)
974+
975+
requests_mock.get(
976+
"https://api.neo4j.io/v1/instances/id0",
977+
status_code=500,
978+
reason="Not Found",
979+
)
980+
981+
with pytest.raises(
982+
AuraApiError,
983+
match="Request for https://api.neo4j.io/v1/instances/id0 failed with status code 500 - Not Found: ``",
984+
):
985+
api.list_instance("id0")
986+
987+
951988
def test_dont_wait_forever(requests_mock: Mocker, caplog: LogCaptureFixture) -> None:
952989
mock_auth_token(requests_mock)
953990
requests_mock.get(

0 commit comments

Comments
 (0)