Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions seam/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ def _handle_error_response(self, response: requests.Response):
if status_code == 401:
raise SeamHttpUnauthorizedError(request_id)

if not self._is_api_error_response(response):
response.raise_for_status()

error = response.json().get("error", {})
error_type = error.get("type", "unknown_error")
error_message = error.get("message", "Unknown error")
Expand All @@ -76,3 +79,34 @@ def _handle_error_response(self, response: requests.Response):
raise SeamHttpInvalidInputError(error_details, status_code, request_id)

raise SeamHttpApiError(error_details, status_code, request_id)

def _is_api_error_response(self, response: requests.Response) -> bool:
try:
content_type = response.headers.get("content-type", "")

if not isinstance(content_type, str) or not content_type.startswith(
"application/json"
):
return False
except ValueError:
return False

try:
data = response.json()
except requests.exceptions.JSONDecodeError:
return False

if not isinstance(data, dict):
return False

error = data.get("error")

if not isinstance(error, dict):
return False

if not isinstance(error.get("type"), str) or not isinstance(
error.get("message"), str
):
return False

return True
Loading