internal/apierror/apierror.go (43 lines of code) (raw):
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package apierror
import (
"fmt"
"net/http"
"net/http/httputil"
"github.com/openai/openai-go/internal/apijson"
"github.com/openai/openai-go/packages/resp"
)
// Error represents an error that originates from the API, i.e. when a request is
// made and the API returns a response with a HTTP status code. Other errors are
// not wrapped by this SDK.
type Error struct {
Code string `json:"code,required"`
Message string `json:"message,required"`
Param string `json:"param,required"`
Type string `json:"type,required"`
// Metadata for the response, check the presence of optional fields with the
// [resp.Field.IsPresent] method.
JSON struct {
Code resp.Field
Message resp.Field
Param resp.Field
Type resp.Field
ExtraFields map[string]resp.Field
raw string
} `json:"-"`
StatusCode int
Request *http.Request
Response *http.Response
}
// Returns the unmodified JSON received from the API
func (r Error) RawJSON() string { return r.JSON.raw }
func (r *Error) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
func (r *Error) Error() string {
// Attempt to re-populate the response body
return fmt.Sprintf("%s %q: %d %s %s", r.Request.Method, r.Request.URL, r.Response.StatusCode, http.StatusText(r.Response.StatusCode), r.JSON.raw)
}
func (r *Error) DumpRequest(body bool) []byte {
if r.Request.GetBody != nil {
r.Request.Body, _ = r.Request.GetBody()
}
out, _ := httputil.DumpRequestOut(r.Request, body)
return out
}
func (r *Error) DumpResponse(body bool) []byte {
out, _ := httputil.DumpResponse(r.Response, body)
return out
}