From adfb1f7c7dbe041efc4b65b2e672f537888758ad Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Sat, 28 Oct 2023 15:12:40 +0100 Subject: [PATCH] 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. --- backend/b2/b2.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/backend/b2/b2.go b/backend/b2/b2.go index b985b07f2..8a5d0a704 100644 --- a/backend/b2/b2.go +++ b/backend/b2/b2.go @@ -9,6 +9,7 @@ import ( "bytes" "context" "crypto/sha1" + "encoding/json" "errors" "fmt" 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 func errorHandler(resp *http.Response) error { - // Decode error response - errResponse := new(api.Error) - err := rest.DecodeJSON(resp, &errResponse) + body, err := rest.ReadBody(resp) 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 == "" { errResponse.Code = "unknown"