github.com/anthropics/anthropic-sdk-go@v1.70.1/internal/apierror/apierror.go (about)

     1  // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
     2  
     3  package apierror
     4  
     5  import (
     6  	"encoding/json"
     7  	"fmt"
     8  	"net/http"
     9  	"net/http/httputil"
    10  
    11  	"github.com/anthropics/anthropic-sdk-go/internal/apijson"
    12  	"github.com/anthropics/anthropic-sdk-go/packages/respjson"
    13  	"github.com/anthropics/anthropic-sdk-go/shared"
    14  )
    15  
    16  // Error represents an error that originates from the API, i.e. when a request is
    17  // made and the API returns a response with a HTTP status code. Other errors are
    18  // not wrapped by this SDK.
    19  type Error struct {
    20  	// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
    21  	JSON struct {
    22  		ExtraFields map[string]respjson.Field
    23  		raw         string
    24  	} `json:"-"`
    25  	StatusCode  int
    26  	Request     *http.Request
    27  	Response    *http.Response
    28  	RequestID   string
    29  	WorkspaceID string
    30  
    31  	errorType shared.ErrorType
    32  }
    33  
    34  // Type returns the error type from the API response body, e.g.
    35  // "rate_limit_error" or "overloaded_error". Returns "" if the
    36  // response body did not contain a recognized error type.
    37  func (r *Error) Type() shared.ErrorType { return r.errorType }
    38  
    39  // Returns the unmodified JSON received from the API
    40  func (r Error) RawJSON() string { return r.JSON.raw }
    41  
    42  func (r *Error) UnmarshalJSON(data []byte) error {
    43  	if !json.Valid(data) {
    44  		r.JSON.raw = string(data)
    45  		return nil
    46  	}
    47  	if err := apijson.UnmarshalRoot(data, r); err != nil {
    48  		return err
    49  	}
    50  	// Extract error type from the standard {"error":{"type":"..."}} envelope.
    51  	var envelope struct {
    52  		Error struct {
    53  			Type shared.ErrorType `json:"type"`
    54  		} `json:"error"`
    55  	}
    56  	if json.Unmarshal(data, &envelope) == nil {
    57  		r.errorType = envelope.Error.Type
    58  	}
    59  	return nil
    60  }
    61  
    62  // UnmarshalAPIJSON marks Error as not apijson-native. See
    63  // [apijson.CustomUnmarshaler].
    64  func (r *Error) UnmarshalAPIJSON(data []byte) error { return r.UnmarshalJSON(data) }
    65  
    66  func (r *Error) Error() string {
    67  	msg := fmt.Sprintf("%s %q: %d %s", r.Request.Method, r.Request.URL, r.Response.StatusCode, http.StatusText(r.Response.StatusCode))
    68  	if r.RequestID != "" {
    69  		msg += fmt.Sprintf(" (Request-ID: %s)", r.RequestID)
    70  	}
    71  	if r.WorkspaceID != "" {
    72  		msg += fmt.Sprintf(" (Workspace-ID: %s)", r.WorkspaceID)
    73  	}
    74  	if body := r.JSON.raw; body != "" {
    75  		msg += " " + body
    76  	}
    77  	return msg
    78  }
    79  
    80  func (r *Error) DumpRequest(body bool) []byte {
    81  	if r.Request.GetBody != nil {
    82  		r.Request.Body, _ = r.Request.GetBody()
    83  	}
    84  	out, _ := httputil.DumpRequestOut(r.Request, body)
    85  	return out
    86  }
    87  
    88  func (r *Error) DumpResponse(body bool) []byte {
    89  	out, _ := httputil.DumpResponse(r.Response, body)
    90  	return out
    91  }