apierror.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2. package apierror
  3. import (
  4. "fmt"
  5. "net/http"
  6. "net/http/httputil"
  7. "github.com/sst/opencode-sdk-go/internal/apijson"
  8. )
  9. // Error represents an error that originates from the API, i.e. when a request is
  10. // made and the API returns a response with a HTTP status code. Other errors are
  11. // not wrapped by this SDK.
  12. type Error struct {
  13. JSON errorJSON `json:"-"`
  14. StatusCode int
  15. Request *http.Request
  16. Response *http.Response
  17. }
  18. // errorJSON contains the JSON metadata for the struct [Error]
  19. type errorJSON struct {
  20. raw string
  21. ExtraFields map[string]apijson.Field
  22. }
  23. func (r *Error) UnmarshalJSON(data []byte) (err error) {
  24. return apijson.UnmarshalRoot(data, r)
  25. }
  26. func (r errorJSON) RawJSON() string {
  27. return r.raw
  28. }
  29. func (r *Error) Error() string {
  30. // Attempt to re-populate the response body
  31. return fmt.Sprintf("%s \"%s\": %d %s %s", r.Request.Method, r.Request.URL, r.Response.StatusCode, http.StatusText(r.Response.StatusCode), r.JSON.RawJSON())
  32. }
  33. func (r *Error) DumpRequest(body bool) []byte {
  34. if r.Request.GetBody != nil {
  35. r.Request.Body, _ = r.Request.GetBody()
  36. }
  37. out, _ := httputil.DumpRequestOut(r.Request, body)
  38. return out
  39. }
  40. func (r *Error) DumpResponse(body bool) []byte {
  41. out, _ := httputil.DumpResponse(r.Response, body)
  42. return out
  43. }