Skip to content

fix: Handle non-json error responses #263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 13, 2025
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