48
error.go
48
error.go
@@ -1,10 +1,13 @@
|
||||
package openai
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// APIError provides error information returned by the OpenAI API.
|
||||
type APIError struct {
|
||||
Code *string `json:"code,omitempty"`
|
||||
Code any `json:"code,omitempty"`
|
||||
Message string `json:"message"`
|
||||
Param *string `json:"param,omitempty"`
|
||||
Type string `json:"type"`
|
||||
@@ -25,6 +28,47 @@ func (e *APIError) Error() string {
|
||||
return e.Message
|
||||
}
|
||||
|
||||
func (e *APIError) UnmarshalJSON(data []byte) (err error) {
|
||||
var rawMap map[string]json.RawMessage
|
||||
err = json.Unmarshal(data, &rawMap)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = json.Unmarshal(rawMap["message"], &e.Message)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = json.Unmarshal(rawMap["type"], &e.Type)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// optional fields
|
||||
if _, ok := rawMap["param"]; ok {
|
||||
err = json.Unmarshal(rawMap["param"], &e.Param)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if _, ok := rawMap["code"]; !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
// if the api returned a number, we need to force an integer
|
||||
// since the json package defaults to float64
|
||||
var intCode int
|
||||
err = json.Unmarshal(rawMap["code"], &intCode)
|
||||
if err == nil {
|
||||
e.Code = intCode
|
||||
return nil
|
||||
}
|
||||
|
||||
return json.Unmarshal(rawMap["code"], &e.Code)
|
||||
}
|
||||
|
||||
func (e *RequestError) Error() string {
|
||||
if e.Err != nil {
|
||||
return e.Err.Error()
|
||||
|
||||
Reference in New Issue
Block a user