1// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
3package apierror
4
5import (
6 "fmt"
7 "net/http"
8 "net/http/httputil"
9
10 "github.com/openai/openai-go/internal/apijson"
11 "github.com/openai/openai-go/packages/respjson"
12)
13
14// Error represents an error that originates from the API, i.e. when a request is
15// made and the API returns a response with a HTTP status code. Other errors are
16// not wrapped by this SDK.
17type Error struct {
18 Code string `json:"code,required"`
19 Message string `json:"message,required"`
20 Param string `json:"param,required"`
21 Type string `json:"type,required"`
22 // JSON contains metadata for fields, check presence with [respjson.Field.Valid].
23 JSON struct {
24 Code respjson.Field
25 Message respjson.Field
26 Param respjson.Field
27 Type respjson.Field
28 ExtraFields map[string]respjson.Field
29 raw string
30 } `json:"-"`
31 StatusCode int
32 Request *http.Request
33 Response *http.Response
34}
35
36// Returns the unmodified JSON received from the API
37func (r Error) RawJSON() string { return r.JSON.raw }
38func (r *Error) UnmarshalJSON(data []byte) error {
39 return apijson.UnmarshalRoot(data, r)
40}
41
42func (r *Error) Error() string {
43 // Attempt to re-populate the response body
44 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)
45}
46
47func (r *Error) DumpRequest(body bool) []byte {
48 if r.Request.GetBody != nil {
49 r.Request.Body, _ = r.Request.GetBody()
50 }
51 out, _ := httputil.DumpRequestOut(r.Request, body)
52 return out
53}
54
55func (r *Error) DumpResponse(body bool) []byte {
56 out, _ := httputil.DumpResponse(r.Response, body)
57 return out
58}