go-openai icon indicating copy to clipboard operation
go-openai copied to clipboard

Expose APIError required

Open OneSeven opened this issue 2 years ago • 3 comments

At present, I don't know how to get the original error message. I need to make conditional judgments based on the original error code to do different things.

Please let me know if you have a solution, thanks

OneSeven avatar Mar 01 '23 13:03 OneSeven

I have the same problem, and I will solve it as soon as possible.

NICEXAI avatar Mar 06 '23 09:03 NICEXAI

I have the same problem, and I will solve it as soon as possible.

Has there been any progress?

OneSeven avatar Mar 13 '23 02:03 OneSeven

I'm sorry, I haven't finished the wrong refactoring work yet because I've been busy recently.

NICEXAI avatar Mar 15 '23 07:03 NICEXAI

This can be solved by using errors.As() as seen in the example README. Check out PR comments #293 for implementation details.

e := &openai.APIError{}
if errors.As(err, &e) {
  switch e.HTTPStatusCode {
    case 401:
      // invalid auth or key (do not retry)
    case 429:
      // rate limiting or engine overload (wait and retry) 
    case 500:
      // openai server error (retry)
    default:
      // unhandled
  }
}

dannysievers avatar May 08 '23 17:05 dannysievers

Closing this due to lack of comments for an extended period.

vvatanabe avatar Jul 01 '23 13:07 vvatanabe