From 242fe2c0ee1cdc8e1a703cac5e6100ec4eb8940b Mon Sep 17 00:00:00 2001 From: Chris Darroch Date: Tue, 18 Jun 2024 17:32:01 -0700 Subject: [PATCH] errors/types.go: correctly wrap retriable errors In commits 2197f76ddeae848814d5297593f6bc0ea0d6e13e and 4fe0a8db4f94725078babc8eadc98f318655a05b of PR #3449 support was added to parse any Retry-After headers provided by a server with a 429 response and delay making further requests until the appropriate time. As part of this work, the NewRetriableLaterError() function in the "errors" package was introduced, which attempts to parse the Retry-After header value. This method accepts an input error, such as that defined by the handleResponse() method of the "lfshttp" package, with the expectation that it will be wrapped in a new retriableLaterError structure. However, the NewRetriableLaterError() function overwrites its input "err" argument when it attempts to parse the header, and so only wraps a "nil" value. We can resolve this problem by using a different name for the errors returned from the header parsing attempts. As well, we rename the value returned from the attempt to parse the header as a timestamped formatted according to RFC 1123, so it does not collide with the name of the standard library's "time" package. --- errors/types.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/errors/types.go b/errors/types.go index ee9f10aa..041e4f39 100644 --- a/errors/types.go +++ b/errors/types.go @@ -416,19 +416,19 @@ func NewRetriableLaterError(err error, header string) error { return nil } - secs, err := strconv.Atoi(header) - if err == nil { + secs, parseErr := strconv.Atoi(header) + if parseErr == nil { return retriableLaterError{ wrappedError: newWrappedError(err, ""), timeAvailable: time.Now().Add(time.Duration(secs) * time.Second), } } - time, err := time.Parse(time.RFC1123, header) - if err == nil { + parseTime, parseErr := time.Parse(time.RFC1123, header) + if parseErr == nil { return retriableLaterError{ wrappedError: newWrappedError(err, ""), - timeAvailable: time, + timeAvailable: parseTime, } }