Skip to content
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

fix(client): correctly assign error properties #759

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Changes from all 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
40 changes: 21 additions & 19 deletions src/openai/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
ProxiesTypes,
RequestOptions,
)
from ._utils import is_given
from ._utils import is_given, is_mapping
from ._version import __version__
from ._streaming import Stream as Stream
from ._streaming import AsyncStream as AsyncStream
Expand Down Expand Up @@ -221,30 +221,31 @@ def _make_status_error(
body: object,
response: httpx.Response,
) -> APIStatusError:
data = body.get("error", body) if is_mapping(body) else body
if response.status_code == 400:
return _exceptions.BadRequestError(err_msg, response=response, body=body)
return _exceptions.BadRequestError(err_msg, response=response, body=data)

if response.status_code == 401:
return _exceptions.AuthenticationError(err_msg, response=response, body=body)
return _exceptions.AuthenticationError(err_msg, response=response, body=data)

if response.status_code == 403:
return _exceptions.PermissionDeniedError(err_msg, response=response, body=body)
return _exceptions.PermissionDeniedError(err_msg, response=response, body=data)

if response.status_code == 404:
return _exceptions.NotFoundError(err_msg, response=response, body=body)
return _exceptions.NotFoundError(err_msg, response=response, body=data)

if response.status_code == 409:
return _exceptions.ConflictError(err_msg, response=response, body=body)
return _exceptions.ConflictError(err_msg, response=response, body=data)

if response.status_code == 422:
return _exceptions.UnprocessableEntityError(err_msg, response=response, body=body)
return _exceptions.UnprocessableEntityError(err_msg, response=response, body=data)

if response.status_code == 429:
return _exceptions.RateLimitError(err_msg, response=response, body=body)
return _exceptions.RateLimitError(err_msg, response=response, body=data)

if response.status_code >= 500:
return _exceptions.InternalServerError(err_msg, response=response, body=body)
return APIStatusError(err_msg, response=response, body=body)
return _exceptions.InternalServerError(err_msg, response=response, body=data)
return APIStatusError(err_msg, response=response, body=data)


class AsyncOpenAI(AsyncAPIClient):
Expand Down Expand Up @@ -431,30 +432,31 @@ def _make_status_error(
body: object,
response: httpx.Response,
) -> APIStatusError:
data = body.get("error", body) if is_mapping(body) else body
if response.status_code == 400:
return _exceptions.BadRequestError(err_msg, response=response, body=body)
return _exceptions.BadRequestError(err_msg, response=response, body=data)

if response.status_code == 401:
return _exceptions.AuthenticationError(err_msg, response=response, body=body)
return _exceptions.AuthenticationError(err_msg, response=response, body=data)

if response.status_code == 403:
return _exceptions.PermissionDeniedError(err_msg, response=response, body=body)
return _exceptions.PermissionDeniedError(err_msg, response=response, body=data)

if response.status_code == 404:
return _exceptions.NotFoundError(err_msg, response=response, body=body)
return _exceptions.NotFoundError(err_msg, response=response, body=data)

if response.status_code == 409:
return _exceptions.ConflictError(err_msg, response=response, body=body)
return _exceptions.ConflictError(err_msg, response=response, body=data)

if response.status_code == 422:
return _exceptions.UnprocessableEntityError(err_msg, response=response, body=body)
return _exceptions.UnprocessableEntityError(err_msg, response=response, body=data)

if response.status_code == 429:
return _exceptions.RateLimitError(err_msg, response=response, body=body)
return _exceptions.RateLimitError(err_msg, response=response, body=data)

if response.status_code >= 500:
return _exceptions.InternalServerError(err_msg, response=response, body=body)
return APIStatusError(err_msg, response=response, body=body)
return _exceptions.InternalServerError(err_msg, response=response, body=data)
return APIStatusError(err_msg, response=response, body=data)


class OpenAIWithRawResponse:
Expand Down