Improve error reporting (#68)
* Provide APIError and use Go's error wrapping * Add generic request error * Fix code formatting
This commit is contained in:
41
error.go
41
error.go
@@ -1,10 +1,37 @@
|
||||
package gogpt
|
||||
|
||||
type ErrorResponse struct {
|
||||
Error *struct {
|
||||
Code *int `json:"code,omitempty"`
|
||||
Message string `json:"message"`
|
||||
Param *string `json:"param,omitempty"`
|
||||
Type string `json:"type"`
|
||||
} `json:"error,omitempty"`
|
||||
import "fmt"
|
||||
|
||||
// APIError provides error information returned by the OpenAI API.
|
||||
type APIError struct {
|
||||
Code *string `json:"code,omitempty"`
|
||||
Message string `json:"message"`
|
||||
Param *string `json:"param,omitempty"`
|
||||
Type string `json:"type"`
|
||||
StatusCode int `json:"-"`
|
||||
}
|
||||
|
||||
// RequestError provides informations about generic request errors.
|
||||
type RequestError struct {
|
||||
StatusCode int
|
||||
Err error
|
||||
}
|
||||
|
||||
type ErrorResponse struct {
|
||||
Error *APIError `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func (e *APIError) Error() string {
|
||||
return e.Message
|
||||
}
|
||||
|
||||
func (e *RequestError) Error() string {
|
||||
if e.Err != nil {
|
||||
return e.Err.Error()
|
||||
}
|
||||
return fmt.Sprintf("status code %d", e.StatusCode)
|
||||
}
|
||||
|
||||
func (e *RequestError) Unwrap() error {
|
||||
return e.Err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user