b2: fix error handler to remove confusing DEBUG messages

On a 404 error, b2 returns an empty body which, before this change,
caused the error handler to try to parse an empty string and give the
following DEBUG message:

    Couldn't decode error response: EOF

This is confusing as it is expected in normal operations and isn't an
error.

This change reads the body of an error response first then tries to
decode it only if it isn't empty, which avoids the confusing DEBUG
message.

This also upgrades failure to read the body or failure to decode the
JSON to ERROR messages as now we are certain that we should have
something to read and decode.
This commit is contained in:
Nick Craig-Wood 2023-10-28 15:12:40 +01:00
parent 6092fe2aaa
commit adfb1f7c7d

View File

@ -9,6 +9,7 @@ import (
"bytes" "bytes"
"context" "context"
"crypto/sha1" "crypto/sha1"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
gohash "hash" gohash "hash"
@ -399,11 +400,18 @@ func (f *Fs) shouldRetry(ctx context.Context, resp *http.Response, err error) (b
// errorHandler parses a non 2xx error response into an error // errorHandler parses a non 2xx error response into an error
func errorHandler(resp *http.Response) error { func errorHandler(resp *http.Response) error {
// Decode error response body, err := rest.ReadBody(resp)
errResponse := new(api.Error)
err := rest.DecodeJSON(resp, &errResponse)
if err != nil { if err != nil {
fs.Debugf(nil, "Couldn't decode error response: %v", err) fs.Errorf(nil, "Couldn't read error out of body: %v", err)
body = nil
}
// Decode error response if there was one - they can be blank
errResponse := new(api.Error)
if len(body) > 0 {
err = json.Unmarshal(body, errResponse)
if err != nil {
fs.Errorf(nil, "Couldn't decode error response: %v", err)
}
} }
if errResponse.Code == "" { if errResponse.Code == "" {
errResponse.Code = "unknown" errResponse.Code = "unknown"