Skip to content

Commit b145006

Browse files
authored
Merge pull request #223 from simvue-io/222-fix-invalid-json-response-handling-in-client
Fix missing invalid JSON response handling
2 parents 36f0a97 + 40bb7a3 commit b145006

File tree

1 file changed

+56
-14
lines changed

1 file changed

+56
-14
lines changed

simvue/client.py

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,10 @@ def get_run_id_from_name(self, name: str) -> str:
125125
)
126126

127127
if response.status_code != 200:
128-
detail = response.json().get("detail", response.text)
128+
try:
129+
detail = response.json().get("detail", response.text)
130+
except requests.exceptions.JSONDecodeError:
131+
detail = response.text
129132
raise RuntimeError(
130133
"Retrieval of run ID from name failed with "
131134
f"status {response.status_code}: {detail}"
@@ -176,7 +179,10 @@ def get_run(self, run_id: str) -> dict[str, typing.Any]:
176179
if response.status_code == 200:
177180
return response.json()
178181

179-
detail = response.json().get("detail", response.text)
182+
try:
183+
detail = response.json().get("detail", response.text)
184+
except requests.exceptions.JSONDecodeError:
185+
detail = response.text
180186

181187
raise RuntimeError(
182188
f"Retrieval of run '{run_id}' failed with status "
@@ -246,7 +252,10 @@ def get_runs(
246252
raise ValueError("Invalid format specified")
247253

248254
if response.status_code != 200:
249-
detail = response.json().get("detail", response.text)
255+
try:
256+
detail = response.json().get("detail", response.text)
257+
except requests.exceptions.JSONDecodeError:
258+
detail = response.text
250259
raise RuntimeError(
251260
f"Run retrieval failed with code {response.status_code}: " f"{detail}"
252261
)
@@ -285,7 +294,10 @@ def delete_run(self, run_identifier: str) -> typing.Optional[dict]:
285294
logger.debug(f"Run '{run_identifier}' deleted successfully")
286295
return response.json()
287296

288-
error_detail = response.json().get("detail", response.text)
297+
try:
298+
error_detail = response.json().get("detail", response.text)
299+
except requests.exceptions.JSONDecodeError:
300+
error_detail = response.text
289301

290302
raise RuntimeError(
291303
f"Deletion of run '{run_identifier}' failed with code"
@@ -418,7 +430,10 @@ def delete_folder(
418430
runs: list[dict] = response.json().get("runs", [])
419431
return runs
420432

421-
detail = response.json().get("detail", response.text)
433+
try:
434+
detail = response.json().get("detail", response.text)
435+
except requests.exceptions.JSONDecodeError:
436+
detail = response.text
422437

423438
raise RuntimeError(
424439
f"Deletion of folder '{folder_name}' failed with"
@@ -450,7 +465,10 @@ def list_artifacts(self, run_id: str) -> list[dict[str, typing.Any]]:
450465
)
451466

452467
if response.status_code != 200:
453-
detail = response.json().get("detail", response.text)
468+
try:
469+
detail = response.json().get("detail", response.text)
470+
except requests.exceptions.JSONDecodeError:
471+
detail = response.text
454472
raise RuntimeError(
455473
f"Retrieval of artifacts for run '{run_id}' failed with "
456474
f"status {response.status_code}: {detail}"
@@ -546,7 +564,10 @@ def get_artifact_as_file(
546564
)
547565

548566
if response.status_code != 200:
549-
detail = response.json().get("detail", response.text)
567+
try:
568+
detail = response.json().get("detail", response.text)
569+
except requests.exceptions.JSONDecodeError:
570+
detail = response.text
550571
raise RuntimeError(
551572
f"Download of artifacts for run '{run_id}' failed with "
552573
f"status {response.status_code}: {detail}"
@@ -653,7 +674,10 @@ def get_artifacts_as_files(
653674
)
654675

655676
if response.status_code != 200:
656-
detail = response.json().get("detail", response.text)
677+
try:
678+
detail = response.json().get("detail", response.text)
679+
except requests.exceptions.JSONDecodeError:
680+
detail = response.text
657681
raise RuntimeError(
658682
f"Download of artifacts for run '{run_id}' failed with "
659683
f"status {response.status_code}: {detail}"
@@ -703,7 +727,10 @@ def get_folder(self, folder_id: str) -> dict[str, typing.Any]:
703727
)
704728

705729
if response.status_code != 200:
706-
detail = response.json().get("detail", response.text)
730+
try:
731+
detail = response.json().get("detail", response.text)
732+
except requests.exceptions.JSONDecodeError:
733+
detail = response.text
707734
raise RuntimeError(
708735
f"Retrieval of folder '{folder_id}' failed with "
709736
f"status {response.status_code}: {detail}"
@@ -744,7 +771,10 @@ def get_folders(
744771
if response.status_code == 200:
745772
return response.json().get("data", [])
746773

747-
detail = response.json().get("detail", response.text)
774+
try:
775+
detail = response.json().get("detail", response.text)
776+
except requests.exceptions.JSONDecodeError:
777+
detail = response.text
748778

749779
raise RuntimeError(
750780
"Retrieval of folders failed with status code "
@@ -778,7 +808,10 @@ def get_metrics_names(self, run_id: str) -> dict[str, typing.Any]:
778808
if response.status_code == 200:
779809
return response.json()
780810

781-
detail = response.json().get("detail", response.text)
811+
try:
812+
detail = response.json().get("detail", response.text)
813+
except requests.exceptions.JSONDecodeError:
814+
detail = response.text
782815

783816
raise RuntimeError(
784817
f"Request for metric names for run '{run_id}' failed with "
@@ -955,7 +988,10 @@ def get_metrics_multiple(
955988
)
956989

957990
if response.status_code != 200:
958-
detail = response.json().get("detail", response.text)
991+
try:
992+
detail = response.json().get("detail", response.text)
993+
except requests.exceptions.JSONDecodeError:
994+
detail = response.text
959995
raise RuntimeError(
960996
f"Retrieval of metrics '{metric_names}' failed for runs '{run_ids}' "
961997
f"with status code {response.status_code}: {detail}"
@@ -1123,7 +1159,10 @@ def get_events(
11231159
if response.status_code == 200:
11241160
return response.json().get("data", [])
11251161

1126-
detail = response.json().get("detail", response.text)
1162+
try:
1163+
detail = response.json().get("detail", response.text)
1164+
except requests.exceptions.JSONDecodeError:
1165+
detail = response.text
11271166

11281167
raise RuntimeError(
11291168
f"Retrieval of events for run '{run_id}' failed with "
@@ -1157,7 +1196,10 @@ def get_alerts(
11571196
response = requests.get(f"{self._url}/api/runs/{run_id}", headers=self._headers)
11581197

11591198
if response.status_code != 200:
1160-
detail = response.json().get("detail", response.text)
1199+
try:
1200+
detail = response.json().get("detail", response.text)
1201+
except requests.exceptions.JSONDecodeError:
1202+
detail = response.text
11611203
raise RuntimeError(
11621204
f"Retrieval of alerts for run '{run_id}' failed with "
11631205
f"status {response.status_code}: {detail}"

0 commit comments

Comments
 (0)