httperr

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package httperr classifies HTTP errors into transient and permanent based on objective HTTP/REST semantics.

Transient errors are those where retrying the same request may succeed: network failures, timeouts, rate limits, server errors (5xx).

Permanent errors are those where retrying is pointless: bad request (400), unauthorized (401), not found (404), etc.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNetwork indicates a transport-level failure: TCP, DNS, TLS.
	// The request never reached the server or the connection was interrupted.
	ErrNetwork = errors.New("httperr: network error")
	// ErrTimeout indicates the request or response exceeded a deadline.
	// Covers both client-side timeouts and HTTP 408 Request Timeout.
	ErrTimeout = errors.New("httperr: timeout")
	// ErrRateLimit indicates the server rejected the request due to
	// rate limiting (HTTP 429 Too Many Requests).
	ErrRateLimit = errors.New("httperr: rate limit")
	// ErrServerError indicates a server-side failure (HTTP 5xx).
	// The server acknowledged the request but failed to process it.
	ErrServerError = errors.New("httperr: server error")
)

Sentinel errors for transient HTTP failures. Use with longrun.TransientRule or errors.Is for classification.

Functions

func Classify

func Classify(err error) error

Classify wraps err with a transient sentinel when the error is objectively retryable per HTTP/REST semantics. Permanent errors are returned as-is.

Classification order:

  1. nil → nil
  2. context.DeadlineExceeded or *url.Error with timeout → ErrTimeout
  3. *net.OpError or *net.DNSError → ErrNetwork
  4. StatusCoder with 408 → ErrTimeout
  5. StatusCoder with 429 → ErrRateLimit
  6. StatusCoder with 5xx → ErrServerError
  7. Everything else → returned unchanged (permanent)

The original error is always preserved in the chain via fmt.Errorf("%w: %w"), so callers can still use errors.As to access the underlying typed error.

Example:

resp, err := httpClient.Do(req)
if err != nil {
    return httperr.Classify(err)
}

func ClassifyStatus

func ClassifyStatus(code int, err error) error

ClassifyStatus maps an HTTP status code to a transient sentinel. Non-transient codes return the original error unchanged.

func TransientErrors

func TransientErrors() []error

TransientErrors returns all sentinel errors that represent objectively transient HTTP failures per REST semantics.

Useful for building github.com/thumbrise/longrun or custom retry logic without depending on pkg/longrun directly.

Returned errors: ErrNetwork, ErrTimeout, ErrRateLimit, ErrServerError.

Types

type StatusCoder

type StatusCoder interface {
	StatusCode() int
}

StatusCoder is implemented by HTTP error types that carry a status code. Standard library does not define this interface, but many HTTP clients (including go-github) return errors with a Response.StatusCode field.

Implement this interface on your HTTP error types to enable automatic classification by Classify.

Jump to

Keyboard shortcuts

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