HTTP: always check status codes

This commit is contained in:
andig 2026-02-07 16:40:29 +01:00
parent 15fd774062
commit 201b93fd32

View file

@ -72,8 +72,8 @@ func (e *StatusError) HasStatus(codes ...int) bool {
// ResponseError turns an HTTP status code into an error
func ResponseError(resp *http.Response) error {
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return &StatusError{resp: resp}
if c := resp.StatusCode; c < 200 || c >= 300 {
return backoff.Permanent(&StatusError{resp: resp})
}
return nil
}
@ -82,15 +82,16 @@ func ResponseError(resp *http.Response) error {
func ReadBody(resp *http.Response) ([]byte, error) {
defer resp.Body.Close()
if err := ResponseError(resp); err != nil {
b, _ := io.ReadAll(resp.Body)
return b, err
}
b, err := io.ReadAll(resp.Body)
if err != nil {
return []byte{}, err
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return b, backoff.Permanent(&StatusError{resp: resp})
}
return b, nil
}