mirror of
https://github.com/hanzo-go/openrouter.git
synced 2026-09-24 00:31:37 +00:00
69 lines
1.4 KiB
Go
69 lines
1.4 KiB
Go
package openrouter
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
)
|
|
|
|
// APIError provides error information returned by the OpenAI API.
|
|
type APIError struct {
|
|
Code any `json:"code,omitempty"`
|
|
Message string `json:"message"`
|
|
Details any `json:"details"`
|
|
HTTPStatusCode int `json:"-"`
|
|
}
|
|
|
|
// RequestError provides informations about generic request errors.
|
|
type RequestError struct {
|
|
HTTPStatusCode int
|
|
Err error
|
|
}
|
|
|
|
type ErrorResponse struct {
|
|
Error *APIError `json:"error,omitempty"`
|
|
}
|
|
|
|
func (e *APIError) Error() string {
|
|
if e.HTTPStatusCode > 0 {
|
|
return fmt.Sprintf("error, status code: %d, message: %s", e.HTTPStatusCode, e.Message)
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
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 {
|
|
return fmt.Sprintf("error, status code: %d, message: %s", e.HTTPStatusCode, e.Err)
|
|
}
|
|
|
|
func (e *RequestError) Unwrap() error {
|
|
return e.Err
|
|
}
|