Skip to content

Commit cb2dc2b

Browse files
committed
docs(readme): add error handler
1 parent 41d771b commit cb2dc2b

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,46 @@ for chunk in response:
7373
print(chunk.choices[0].delta)
7474
```
7575

76+
### 异常处理
77+
78+
When the library is unable to connect to the API (for example, due to network connection problems or a timeout), a subclass of `openai.APIConnectionError` is raised.
79+
80+
When the API returns a non-success status code (that is, 4xx or 5xx
81+
response), a subclass of `openai.APIStatusError` is raised, containing `status_code` and `response` properties.
82+
83+
All errors inherit from `openai.APIError`.
84+
85+
```python
86+
import openai
87+
from openai import OpenAI
88+
89+
client = OpenAI()
90+
91+
try:
92+
client.fine_tunes.create(
93+
training_file="file-XGinujblHPwGLSztz8cPS8XY",
94+
)
95+
except openai.APIConnectionError as e:
96+
print("The server could not be reached")
97+
print(e.__cause__) # an underlying Exception, likely raised within httpx.
98+
except openai.RateLimitError as e:
99+
print("A 429 status code was received; we should back off a bit.")
100+
except openai.APIStatusError as e:
101+
print("Another non-200-range status code was received")
102+
print(e.status_code)
103+
print(e.response)
104+
```
105+
106+
Error codes are as followed:
107+
108+
| Status Code | Error Type |
109+
| ----------- | -------------------------- |
110+
| 400 | `BadRequestError` |
111+
| 401 | `AuthenticationError` |
112+
| 403 | `PermissionDeniedError` |
113+
| 404 | `NotFoundError` |
114+
| 422 | `UnprocessableEntityError` |
115+
| 429 | `RateLimitError` |
116+
| >=500 | `InternalServerError` |
117+
| N/A | `APIConnectionError` |
118+

0 commit comments

Comments
 (0)