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.
This commit is contained in:
2026-05-23 13:47:13 +03:00
parent 2d4a06e715
commit e8c4577c6f
5 changed files with 48 additions and 12 deletions

View File

@@ -1,6 +1,7 @@
package httpx
import (
"errors"
"fmt"
"net/http"
@@ -18,6 +19,11 @@ var (
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").