Skip to content

Commit a2ed4fc

Browse files
Handle non-json error responses
1 parent a4c6bad commit a2ed4fc

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

seam/client.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ def _handle_error_response(self, response: requests.Response):
6161
if status_code == 401:
6262
raise SeamHttpUnauthorizedError(request_id)
6363

64+
if not self._is_api_error_response(response):
65+
response.raise_for_status()
66+
6467
error = response.json().get("error", {})
6568
error_type = error.get("type", "unknown_error")
6669
error_message = error.get("message", "Unknown error")
@@ -76,3 +79,30 @@ def _handle_error_response(self, response: requests.Response):
7679
raise SeamHttpInvalidInputError(error_details, status_code, request_id)
7780

7881
raise SeamHttpApiError(error_details, status_code, request_id)
82+
83+
def _is_api_error_response(self, response: requests.Response) -> bool:
84+
try:
85+
content_type = response.headers.get("content-type", "")
86+
if not isinstance(content_type, str) or not content_type.startswith(
87+
"application/json"
88+
):
89+
return False
90+
91+
data = response.json()
92+
except Exception:
93+
return False
94+
95+
if not isinstance(data, dict):
96+
return False
97+
98+
error = data.get("error")
99+
100+
if not isinstance(error, dict):
101+
return False
102+
103+
if not isinstance(error.get("type"), str) or not isinstance(
104+
error.get("message"), str
105+
):
106+
return False
107+
108+
return True

0 commit comments

Comments
 (0)