providerhttp

package
v0.0.327 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 11, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseRetryAfter

func ParseRetryAfter(header http.Header, now time.Time) (time.Duration, bool)

ParseRetryAfter parses Retry-After metadata from HTTP headers. It supports:

  • Retry-After-Ms / retry-after-ms: integer milliseconds
  • Retry-After: integer seconds
  • Retry-After: HTTP-date

func ParseRetryAfterMillisecondsValue

func ParseRetryAfterMillisecondsValue(value string) (time.Duration, bool)

ParseRetryAfterMillisecondsValue parses a Retry-After-Ms header value.

func ParseRetryAfterValue

func ParseRetryAfterValue(value string, now time.Time) (time.Duration, bool)

ParseRetryAfterValue parses a Retry-After header value as either seconds or an HTTP-date. now is used for deterministic tests; pass a zero time to use time.Now().

func ReadBodyAndClose

func ReadBodyAndClose(resp *http.Response, limit int64) []byte

ReadBodyAndClose reads resp.Body and closes it. If limit is positive, at most limit bytes are returned. Read errors are intentionally ignored because status error bodies are best-effort diagnostics.

func RetryableStatus

func RetryableStatus(statusCode int) bool

RetryableStatus reports whether statusCode is commonly transient and worth retrying for provider/API calls.

Types

type StatusError

type StatusError struct {
	Provider   string
	StatusCode int
	Status     string
	Body       string
	Header     http.Header
	Message    string
}

StatusError captures a non-success HTTP response from a provider while preserving response headers for retry/backoff decisions (notably Retry-After). The Error text remains provider-oriented for user display.

func NewStatusError

func NewStatusError(provider string, resp *http.Response, body []byte) *StatusError

NewStatusError returns a typed status error using the default "<provider> API error (status N): body" message shape. If provider is empty, the message starts with "API error".

func NewStatusErrorFromResponse

func NewStatusErrorFromResponse(provider string, resp *http.Response) *StatusError

NewStatusErrorFromResponse reads and closes resp.Body, then returns a typed status error. It is intended for the common non-2xx path where callers do not need to inspect the body separately.

func NewStatusErrorMessage

func NewStatusErrorMessage(message string, resp *http.Response, body []byte) *StatusError

NewStatusErrorMessage returns a typed status error with a caller-supplied display message. Use this when existing code has a more specific error string that should be preserved while still attaching status/header metadata.

func NewStatusErrorMessageFromResponse

func NewStatusErrorMessageFromResponse(resp *http.Response, buildMessage func(body []byte) string) *StatusError

NewStatusErrorMessageFromResponse reads and closes resp.Body, then returns a typed status error with a caller-built display message.

func NewStatusErrorMessageString

func NewStatusErrorMessageString(message string, statusCode int, status string, header http.Header, body string) *StatusError

NewStatusErrorMessageString is like NewStatusErrorMessage but accepts already-normalized body text.

func NewStatusErrorMessagef

func NewStatusErrorMessagef(resp *http.Response, body []byte, format string, args ...any) *StatusError

NewStatusErrorMessagef returns a typed status error with a formatted display message.

func NewStatusErrorString

func NewStatusErrorString(provider string, statusCode int, status string, header http.Header, body string) *StatusError

NewStatusErrorString is like NewStatusError but accepts already-normalized body text.

func (*StatusError) Error

func (e *StatusError) Error() string

func (*StatusError) HTTPStatusCode

func (e *StatusError) HTTPStatusCode() int

HTTPStatusCode exposes the status code for generic retryability checks.

func (*StatusError) RetryAfterDelay

func (e *StatusError) RetryAfterDelay() (time.Duration, bool)

RetryAfterDelay exposes Retry-After metadata for generic retry/backoff logic.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL