Description
The official Open-AI documentation gives clear instruction on handling API errors here
Particularly they quote
Handling errors
We advise you to programmatically handle errors returned by the API. To do so, you may want to use a code snippet like below:
Unfortunately the status code is embedded into an error string here
forcing consumers to string match when looking for status codes from the server.
Ideally the library would utilize its existing APIError struct that currently satisfies the error interface so that it can maintain its current structure with the possibility of type conversion to pull out status codes.
The library almost allows for this but overwrites the struct with an anonymous error string in the last moment
To match the OpenAI documentation, ideally consumer code would look something like this
respErr, ok := err.(*openai.APIError)
if !ok {
// handle unknown error
}
switch respErr.StatusCode {
case 401:
// invalid auth or key
case 429:
// rate limiting or engine overload (probably an additional string check required here to determine which 429)
case 500:
// openai server error
default:
// unhandled
}