-
Notifications
You must be signed in to change notification settings - Fork 7
/
error.go
38 lines (34 loc) · 876 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
31
32
33
34
35
36
37
38
package openai
import (
"encoding/json"
"fmt"
"net/http"
)
// APIError is a representation of the error body returned by OpenAI apis, and
// will be returned by Client calls if encountered.
type APIError struct {
Data struct {
Message string `json:"message"`
Type string `json:"type"`
Param any `json:"param"`
Code any `json:"code"`
} `json:"error"`
}
// Error implements error
func (c *APIError) Error() string {
return fmt.Sprintf("openai remote error: %s: %s", c.Data.Type, c.Data.Message)
}
// Parse the error as a roundtripper requestfunc and return the APIError if one
// was encountered.
func parseOpenAIError(r *http.Response) (*http.Response, error) {
if r.StatusCode > 399 {
defer r.Body.Close()
var e APIError
err := json.NewDecoder(r.Body).Decode(&e)
if err != nil {
return r, err
}
return r, &e
}
return r, nil
}