ResponseErrorFromKeyPool translates a *keypool.Error into a developer-facing ResponseError shaped for the OpenAI API.
(keyPoolErr *keypool.Error)
| 73 | // ResponseErrorFromKeyPool translates a *keypool.Error into |
| 74 | // a developer-facing ResponseError shaped for the OpenAI API. |
| 75 | func ResponseErrorFromKeyPool(keyPoolErr *keypool.Error) *ResponseError { |
| 76 | switch keyPoolErr.Kind { |
| 77 | case keypool.ErrorKindPermanent: |
| 78 | return NewResponseError( |
| 79 | keyPoolErr.Error(), |
| 80 | OpenAIErrTypeAPI, |
| 81 | OpenAIErrCodeServer, |
| 82 | http.StatusBadGateway, |
| 83 | keyPoolErr.RetryAfter, |
| 84 | ) |
| 85 | case keypool.ErrorKindRateLimited: |
| 86 | return NewResponseError( |
| 87 | keyPoolErr.Error(), |
| 88 | OpenAIErrTypeRateLimit, |
| 89 | OpenAIErrCodeRateLimit, |
| 90 | http.StatusTooManyRequests, |
| 91 | keyPoolErr.RetryAfter, |
| 92 | ) |
| 93 | default: |
| 94 | // Fall back to a generic 502. |
| 95 | return NewResponseError( |
| 96 | keyPoolErr.Error(), |
| 97 | OpenAIErrTypeAPI, |
| 98 | OpenAIErrCodeServer, |
| 99 | http.StatusBadGateway, |
| 100 | keyPoolErr.RetryAfter, |
| 101 | ) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // ResponseErrorFromAPIError converts an OpenAI SDK error into a |
| 106 | // ResponseError. Returns nil if err is not an *openai.Error. |