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/anthropics/anthropic-sdk-go/internal/apijson"
11 "github.com/anthropics/anthropic-sdk-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 // JSON contains metadata for fields, check presence with [respjson.Field.Valid].
19 JSON struct {
20 ExtraFields map[string]respjson.Field
21 raw string
22 } `json:"-"`
23 StatusCode int
24 Request *http.Request
25 Response *http.Response
26}
27
28// Returns the unmodified JSON received from the API
29func (r Error) RawJSON() string { return r.JSON.raw }
30func (r *Error) UnmarshalJSON(data []byte) error {
31 return apijson.UnmarshalRoot(data, r)
32}
33
34func (r *Error) Error() string {
35 // Attempt to re-populate the response body
36 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)
37}
38
39func (r *Error) DumpRequest(body bool) []byte {
40 if r.Request.GetBody != nil {
41 r.Request.Body, _ = r.Request.GetBody()
42 }
43 out, _ := httputil.DumpRequestOut(r.Request, body)
44 return out
45}
46
47func (r *Error) DumpResponse(body bool) []byte {
48 out, _ := httputil.DumpResponse(r.Response, body)
49 return out
50}