Files
httpx/error.go
Aleksey Shakhmatov e8c4577c6f Return ErrResponseTooLarge instead of truncating response body
WithMaxResponseBody wrapped the body in io.LimitedReader, which returns EOF
at the cap, so Bytes/JSON/XML silently returned a truncated body with a nil
error despite the documented contract. Read one byte past the limit and
return the new ErrResponseTooLarge sentinel when exceeded; bodies exactly at
the limit still succeed.
2026-05-23 13:47:13 +03:00

56 lines
1.8 KiB
Go

package httpx
import (
"errors"
"fmt"
"net/http"
"git.codelab.vc/pkg/httpx/balancer"
"git.codelab.vc/pkg/httpx/circuitbreaker"
"git.codelab.vc/pkg/httpx/retry"
)
// Sentinel errors returned by httpx components.
// These are aliases for the canonical errors defined in sub-packages,
// so that errors.Is works regardless of which import the caller uses.
var (
ErrRetryExhausted = retry.ErrRetryExhausted
ErrCircuitOpen = circuitbreaker.ErrCircuitOpen
ErrNoHealthy = balancer.ErrNoHealthy
)
// ErrResponseTooLarge is returned when reading a response body that exceeds
// the limit configured via WithMaxResponseBody. Any bytes read up to the
// limit are returned alongside the error.
var ErrResponseTooLarge = errors.New("httpx: response body exceeds configured limit")
// Error provides structured error information for failed HTTP operations.
type Error struct {
// Op is the operation that failed (e.g. "Get", "Do").
Op string
// URL is the originally-requested URL.
URL string
// Endpoint is the resolved endpoint URL (after balancing).
Endpoint string
// StatusCode is the HTTP status code, if a response was received.
StatusCode int
// Retries is the number of retry attempts made.
Retries int
// Err is the underlying error.
Err error
}
func (e *Error) Error() string {
if e.Endpoint != "" && e.Endpoint != e.URL {
return fmt.Sprintf("httpx: %s %s (endpoint %s): %v", e.Op, e.URL, e.Endpoint, e.Err)
}
return fmt.Sprintf("httpx: %s %s: %v", e.Op, e.URL, e.Err)
}
func (e *Error) Unwrap() error { return e.Err }
// ErrorMapper maps an HTTP response to an error. If the response is
// acceptable, the mapper should return nil. Used by Client to convert
// non-successful HTTP responses into Go errors.
type ErrorMapper func(resp *http.Response) error