88 "fmt"
99 bytesext "github.com/go-playground/pkg/v5/bytes"
1010 errorsext "github.com/go-playground/pkg/v5/errors"
11- ioext "github.com/go-playground/pkg/v5/io"
1211 resultext "github.com/go-playground/pkg/v5/values/result"
13- "io"
1412 "net/http"
1513 "strconv"
1614)
@@ -33,11 +31,20 @@ var (
3331
3432// ErrRetryableStatusCode can be used to indicate a retryable HTTP status code was encountered as an error.
3533type ErrRetryableStatusCode struct {
36- StatusCode int
34+ Response * http. Response
3735}
3836
3937func (e ErrRetryableStatusCode ) Error () string {
40- return fmt .Sprintf ("retryable HTTP status code encountered: %d" , e .StatusCode )
38+ return fmt .Sprintf ("retryable HTTP status code encountered: %d" , e .Response .StatusCode )
39+ }
40+
41+ // ErrUnexpectedResponse can be used to indicate an unexpected response was encountered as an error and provide access to the *http.Response.
42+ type ErrUnexpectedResponse struct {
43+ Response * http.Response
44+ }
45+
46+ func (e ErrUnexpectedResponse ) Error () string {
47+ return "unexpected response encountered"
4148}
4249
4350// IsRetryableStatusCode returns if the provided status code is considered retryable.
@@ -77,7 +84,7 @@ func DoRetryableResponse(ctx context.Context, onRetryFn errorsext.OnRetryFn[erro
7784 }
7885
7986 if isRetryableStatusCode (resp .StatusCode ) {
80- opt := onRetryFn (ctx , ErrRetryableStatusCode {StatusCode : resp . StatusCode }, strconv .Itoa (resp .StatusCode ), attempt )
87+ opt := onRetryFn (ctx , ErrRetryableStatusCode {Response : resp }, strconv .Itoa (resp .StatusCode ), attempt )
8188 if opt .IsSome () {
8289 return resultext.Err [* http.Response , error ](opt .Unwrap ())
8390 }
@@ -106,9 +113,7 @@ func DoRetryable[T any](ctx context.Context, isRetryableFn errorsext.IsRetryable
106113 defer resp .Body .Close ()
107114
108115 if resp .StatusCode != expectedResponseCode {
109- b , _ := io .ReadAll (ioext .LimitReader (resp .Body , maxMemory ))
110- err := fmt .Errorf ("invalid response status code: %d body: %s" , resp .StatusCode , string (b ))
111- return resultext.Err [T , error ](err )
116+ return resultext.Err [T , error ](ErrUnexpectedResponse {Response : resp })
112117 }
113118
114119 data , err := DecodeResponse [T ](resp , maxMemory )
0 commit comments