b2: fix decoding of 404 errors producing "Couldn't decode error response: EOF"

See: https://forum.rclone.org/t/move-to-b2-creates-empty-file-versions/23420/
This commit is contained in:
Nick Craig-Wood 2021-04-11 11:56:57 +01:00
parent cb30a8c80e
commit ee87bf19c8

View File

@ -9,6 +9,7 @@ import (
"bytes"
"context"
"crypto/sha1"
"encoding/json"
"fmt"
gohash "hash"
"io"
@ -344,12 +345,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 {
body, err := rest.ReadBody(resp)
if err != nil {
return errors.Wrap(err, "error reading error out of body")
}
// Decode error response
errResponse := new(api.Error)
err := rest.DecodeJSON(resp, &errResponse)
if len(body) > 0 {
err := json.Unmarshal(body, &errResponse)
if err != nil {
fs.Debugf(nil, "Couldn't decode error response: %v", err)
}
}
if errResponse.Code == "" {
errResponse.Code = "unknown"
}
@ -357,7 +364,7 @@ func errorHandler(resp *http.Response) error {
errResponse.Status = resp.StatusCode
}
if errResponse.Message == "" {
errResponse.Message = "Unknown " + resp.Status
errResponse.Message = fmt.Sprintf("Unknown: %s: %s", resp.Status, body)
}
return errResponse
}