forked from otiai10/openaigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
30 lines (25 loc) · 759 Bytes
/
error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package openaigo
import (
"encoding/json"
"fmt"
"net/http"
)
type ErrorResponseBody struct {
Error ErrorEntry `json:"error"`
}
type ErrorEntry struct {
Message string `json:"message"`
Type string `json:"type"`
Param interface{} `json:"param"` // TODO: typing
Code interface{} `json:"code"` // TODO: typing
}
func (err ErrorEntry) Error() string {
return fmt.Sprintf("%v: %v (param: %v, code: %v)", err.Type, err.Message, err.Param, err.Code)
}
func (client *Client) apiError(res *http.Response) error {
errbody := ErrorResponseBody{}
if err := json.NewDecoder(res.Body).Decode(&errbody); err != nil {
return fmt.Errorf("failed to decode error body: %v", err)
}
return fmt.Errorf("openai api error: %v", errbody.Error)
}