openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
src/openai/_exceptions.py
127lines · modecode
| 1 | # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | from typing import Any, Optional, cast |
| 6 | from typing_extensions import Literal |
| 7 | |
| 8 | import httpx |
| 9 | |
| 10 | from ._utils import is_dict |
| 11 | from ._models import construct_type |
| 12 | |
| 13 | __all__ = [ |
| 14 | "BadRequestError", |
| 15 | "AuthenticationError", |
| 16 | "PermissionDeniedError", |
| 17 | "NotFoundError", |
| 18 | "ConflictError", |
| 19 | "UnprocessableEntityError", |
| 20 | "RateLimitError", |
| 21 | "InternalServerError", |
| 22 | ] |
| 23 | |
| 24 | |
| 25 | class OpenAIError(Exception): |
| 26 | pass |
| 27 | |
| 28 | |
| 29 | class APIError(OpenAIError): |
| 30 | message: str |
| 31 | request: httpx.Request |
| 32 | |
| 33 | body: object | None |
| 34 | """The API response body. |
| 35 | |
| 36 | If the API responded with a valid JSON structure then this property will be the |
| 37 | decoded result. |
| 38 | |
| 39 | If it isn't a valid JSON structure then this will be the raw response. |
| 40 | |
| 41 | If there was no response associated with this error then it will be `None`. |
| 42 | """ |
| 43 | |
| 44 | code: Optional[str] = None |
| 45 | param: Optional[str] = None |
| 46 | type: Optional[str] |
| 47 | |
| 48 | def __init__(self, message: str, request: httpx.Request, *, body: object | None) -> None: |
| 49 | super().__init__(message) |
| 50 | self.request = request |
| 51 | self.message = message |
| 52 | self.body = body |
| 53 | |
| 54 | if is_dict(body): |
| 55 | self.code = cast(Any, construct_type(type_=Optional[str], value=body.get("code"))) |
| 56 | self.param = cast(Any, construct_type(type_=Optional[str], value=body.get("param"))) |
| 57 | self.type = cast(Any, construct_type(type_=str, value=body.get("type"))) |
| 58 | else: |
| 59 | self.code = None |
| 60 | self.param = None |
| 61 | self.type = None |
| 62 | |
| 63 | |
| 64 | class APIResponseValidationError(APIError): |
| 65 | response: httpx.Response |
| 66 | status_code: int |
| 67 | |
| 68 | def __init__(self, response: httpx.Response, body: object | None, *, message: str | None = None) -> None: |
| 69 | super().__init__(message or "Data returned by API invalid for expected schema.", response.request, body=body) |
| 70 | self.response = response |
| 71 | self.status_code = response.status_code |
| 72 | |
| 73 | |
| 74 | class APIStatusError(APIError): |
| 75 | """Raised when an API response has a status code of 4xx or 5xx.""" |
| 76 | |
| 77 | response: httpx.Response |
| 78 | status_code: int |
| 79 | request_id: str | None |
| 80 | |
| 81 | def __init__(self, message: str, *, response: httpx.Response, body: object | None) -> None: |
| 82 | super().__init__(message, response.request, body=body) |
| 83 | self.response = response |
| 84 | self.status_code = response.status_code |
| 85 | self.request_id = response.headers.get("x-request-id") |
| 86 | |
| 87 | |
| 88 | class APIConnectionError(APIError): |
| 89 | def __init__(self, *, message: str = "Connection error.", request: httpx.Request) -> None: |
| 90 | super().__init__(message, request, body=None) |
| 91 | |
| 92 | |
| 93 | class APITimeoutError(APIConnectionError): |
| 94 | def __init__(self, request: httpx.Request) -> None: |
| 95 | super().__init__(message="Request timed out.", request=request) |
| 96 | |
| 97 | |
| 98 | class BadRequestError(APIStatusError): |
| 99 | status_code: Literal[400] = 400 # pyright: ignore[reportIncompatibleVariableOverride] |
| 100 | |
| 101 | |
| 102 | class AuthenticationError(APIStatusError): |
| 103 | status_code: Literal[401] = 401 # pyright: ignore[reportIncompatibleVariableOverride] |
| 104 | |
| 105 | |
| 106 | class PermissionDeniedError(APIStatusError): |
| 107 | status_code: Literal[403] = 403 # pyright: ignore[reportIncompatibleVariableOverride] |
| 108 | |
| 109 | |
| 110 | class NotFoundError(APIStatusError): |
| 111 | status_code: Literal[404] = 404 # pyright: ignore[reportIncompatibleVariableOverride] |
| 112 | |
| 113 | |
| 114 | class ConflictError(APIStatusError): |
| 115 | status_code: Literal[409] = 409 # pyright: ignore[reportIncompatibleVariableOverride] |
| 116 | |
| 117 | |
| 118 | class UnprocessableEntityError(APIStatusError): |
| 119 | status_code: Literal[422] = 422 # pyright: ignore[reportIncompatibleVariableOverride] |
| 120 | |
| 121 | |
| 122 | class RateLimitError(APIStatusError): |
| 123 | status_code: Literal[429] = 429 # pyright: ignore[reportIncompatibleVariableOverride] |
| 124 | |
| 125 | |
| 126 | class InternalServerError(APIStatusError): |
| 127 | pass |