From 53bbe891d377c25d10d3c99958f1de27aafa6e14 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Fri, 14 Feb 2025 14:47:09 +0000 Subject: [PATCH] lib/rest: retry errors due to failed JSON decoding - fixes #8383 These errors are almost always caused by an overloaded proxy somewhere in the chain returning HTML so they are worth retrying. --- lib/rest/rest.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/rest/rest.go b/lib/rest/rest.go index a6a94070d..42423062c 100644 --- a/lib/rest/rest.go +++ b/lib/rest/rest.go @@ -17,6 +17,7 @@ import ( "sync" "github.com/rclone/rclone/fs" + "github.com/rclone/rclone/fs/fserrors" "github.com/rclone/rclone/lib/readers" ) @@ -189,7 +190,13 @@ func checkDrainAndClose(r io.ReadCloser, err *error) { func DecodeJSON(resp *http.Response, result interface{}) (err error) { defer checkDrainAndClose(resp.Body, &err) decoder := json.NewDecoder(resp.Body) - return decoder.Decode(result) + err = decoder.Decode(result) + if err != nil { + // Retry this as it is likely some overloaded server sending HTML instead of JSON + contentType := resp.Header.Get("Content-Type") + err = fserrors.RetryError(fmt.Errorf("failed to decode JSON from Content-Type: %q: %v", contentType, err)) + } + return err } // DecodeXML decodes resp.Body into result