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