openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
openai/error.py
165lines · modecode
| 1 | from __future__ import absolute_import, division, print_function |
| 2 | |
| 3 | import openai |
| 4 | from openai.six import python_2_unicode_compatible |
| 5 | |
| 6 | |
| 7 | @python_2_unicode_compatible |
| 8 | class OpenAIError(Exception): |
| 9 | def __init__( |
| 10 | self, |
| 11 | message=None, |
| 12 | http_body=None, |
| 13 | http_status=None, |
| 14 | json_body=None, |
| 15 | headers=None, |
| 16 | code=None, |
| 17 | ): |
| 18 | super(OpenAIError, self).__init__(message) |
| 19 | |
| 20 | if http_body and hasattr(http_body, "decode"): |
| 21 | try: |
| 22 | http_body = http_body.decode("utf-8") |
| 23 | except BaseException: |
| 24 | http_body = ( |
| 25 | "<Could not decode body as utf-8. " |
| 26 | "Please report to support@openai.com>" |
| 27 | ) |
| 28 | |
| 29 | self._message = message |
| 30 | self.http_body = http_body |
| 31 | self.http_status = http_status |
| 32 | self.json_body = json_body |
| 33 | self.headers = headers or {} |
| 34 | self.code = code |
| 35 | self.request_id = self.headers.get("request-id", None) |
| 36 | self.error = self.construct_error_object() |
| 37 | self.organization = self.headers.get("openai-organization", None) |
| 38 | |
| 39 | def __str__(self): |
| 40 | msg = self._message or "<empty message>" |
| 41 | if self.request_id is not None: |
| 42 | return u"Request {0}: {1}".format(self.request_id, msg) |
| 43 | else: |
| 44 | return msg |
| 45 | |
| 46 | # Returns the underlying `Exception` (base class) message, which is usually |
| 47 | # the raw message returned by OpenAI's API. This was previously available |
| 48 | # in python2 via `error.message`. Unlike `str(error)`, it omits "Request |
| 49 | # req_..." from the beginning of the string. |
| 50 | @property |
| 51 | def user_message(self): |
| 52 | return self._message |
| 53 | |
| 54 | def __repr__(self): |
| 55 | return "%s(message=%r, http_status=%r, request_id=%r)" % ( |
| 56 | self.__class__.__name__, |
| 57 | self._message, |
| 58 | self.http_status, |
| 59 | self.request_id, |
| 60 | ) |
| 61 | |
| 62 | def construct_error_object(self): |
| 63 | if ( |
| 64 | self.json_body is None |
| 65 | or "error" not in self.json_body |
| 66 | or not isinstance(self.json_body["error"], dict) |
| 67 | ): |
| 68 | return None |
| 69 | |
| 70 | return openai.api_resources.error_object.ErrorObject.construct_from( |
| 71 | self.json_body["error"], openai.api_key |
| 72 | ) |
| 73 | |
| 74 | |
| 75 | class APIError(OpenAIError): |
| 76 | pass |
| 77 | |
| 78 | |
| 79 | class TryAgain(OpenAIError): |
| 80 | pass |
| 81 | |
| 82 | |
| 83 | class APIConnectionError(OpenAIError): |
| 84 | def __init__( |
| 85 | self, |
| 86 | message, |
| 87 | http_body=None, |
| 88 | http_status=None, |
| 89 | json_body=None, |
| 90 | headers=None, |
| 91 | code=None, |
| 92 | should_retry=False, |
| 93 | ): |
| 94 | super(APIConnectionError, self).__init__( |
| 95 | message, http_body, http_status, json_body, headers, code |
| 96 | ) |
| 97 | self.should_retry = should_retry |
| 98 | |
| 99 | |
| 100 | class OpenAIErrorWithParamCode(OpenAIError): |
| 101 | def __repr__(self): |
| 102 | return "%s(message=%r, param=%r, code=%r, http_status=%r, " "request_id=%r)" % ( |
| 103 | self.__class__.__name__, |
| 104 | self._message, |
| 105 | self.param, |
| 106 | self.code, |
| 107 | self.http_status, |
| 108 | self.request_id, |
| 109 | ) |
| 110 | |
| 111 | |
| 112 | class CardError(OpenAIErrorWithParamCode): |
| 113 | def __init__( |
| 114 | self, |
| 115 | message, |
| 116 | param, |
| 117 | code, |
| 118 | http_body=None, |
| 119 | http_status=None, |
| 120 | json_body=None, |
| 121 | headers=None, |
| 122 | ): |
| 123 | super(CardError, self).__init__( |
| 124 | message, http_body, http_status, json_body, headers, code |
| 125 | ) |
| 126 | self.param = param |
| 127 | |
| 128 | |
| 129 | class IdempotencyError(OpenAIError): |
| 130 | pass |
| 131 | |
| 132 | |
| 133 | class InvalidRequestError(OpenAIErrorWithParamCode): |
| 134 | def __init__( |
| 135 | self, |
| 136 | message, |
| 137 | param, |
| 138 | code=None, |
| 139 | http_body=None, |
| 140 | http_status=None, |
| 141 | json_body=None, |
| 142 | headers=None, |
| 143 | ): |
| 144 | super(InvalidRequestError, self).__init__( |
| 145 | message, http_body, http_status, json_body, headers, code |
| 146 | ) |
| 147 | self.param = param |
| 148 | |
| 149 | |
| 150 | class AuthenticationError(OpenAIError): |
| 151 | pass |
| 152 | |
| 153 | |
| 154 | class PermissionError(OpenAIError): |
| 155 | pass |
| 156 | |
| 157 | |
| 158 | class RateLimitError(OpenAIError): |
| 159 | pass |
| 160 | |
| 161 | |
| 162 | class SignatureVerificationError(OpenAIError): |
| 163 | def __init__(self, message, sig_header, http_body=None): |
| 164 | super(SignatureVerificationError, self).__init__(message, http_body) |
| 165 | self.sig_header = sig_header |
| 166 | |