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.
This commit is contained in:
Chris Darroch 2024-06-18 17:32:01 -07:00
parent 43c0a1fae8
commit 242fe2c0ee

@ -416,19 +416,19 @@ func NewRetriableLaterError(err error, header string) error {
return nil return nil
} }
secs, err := strconv.Atoi(header) secs, parseErr := strconv.Atoi(header)
if err == nil { if parseErr == nil {
return retriableLaterError{ return retriableLaterError{
wrappedError: newWrappedError(err, ""), wrappedError: newWrappedError(err, ""),
timeAvailable: time.Now().Add(time.Duration(secs) * time.Second), timeAvailable: time.Now().Add(time.Duration(secs) * time.Second),
} }
} }
time, err := time.Parse(time.RFC1123, header) parseTime, parseErr := time.Parse(time.RFC1123, header)
if err == nil { if parseErr == nil {
return retriableLaterError{ return retriableLaterError{
wrappedError: newWrappedError(err, ""), wrappedError: newWrappedError(err, ""),
timeAvailable: time, timeAvailable: parseTime,
} }
} }