Skip to content

Commit ca9426b

Browse files
author
Dan Moseley
committed
Include RetryAfter in AbuseRateLimitError.Error() output
When RetryAfter is populated, append '[retry after <duration>]' to the error string. This makes the retry information visible to callers that surface .Error() (e.g. logging, AI agent frameworks) without needing to use errors.As to inspect the struct directly. This is consistent with RateLimitError.Error() which already includes timing via formatRateReset. Fixes google#4180.
1 parent f019b7f commit ca9426b

2 files changed

Lines changed: 51 additions & 12 deletions

File tree

‎github/github.go‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,9 +1363,13 @@ type AbuseRateLimitError struct {
13631363
}
13641364

13651365
func (r *AbuseRateLimitError) Error() string {
1366-
return fmt.Sprintf("%v %v: %v %v",
1366+
retryInfo := ""
1367+
if r.RetryAfter != nil && *r.RetryAfter > 0 {
1368+
retryInfo = fmt.Sprintf(" [retry after %v]", r.RetryAfter.Round(time.Second))
1369+
}
1370+
return fmt.Sprintf("%v %v: %v %v%v",
13671371
r.Response.Request.Method, sanitizeURL(r.Response.Request.URL),
1368-
r.Response.StatusCode, r.Message)
1372+
r.Response.StatusCode, r.Message, retryInfo)
13691373
}
13701374

13711375
// Is returns whether the provided error equals this error.

‎github/github_test.go‎

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3277,16 +3277,51 @@ func TestAbuseRateLimitError(t *testing.T) {
32773277
t.Fatal(err)
32783278
}
32793279

3280-
r := &AbuseRateLimitError{
3281-
Response: &http.Response{
3282-
Request: &http.Request{Method: "PUT", URL: u},
3283-
StatusCode: http.StatusTooManyRequests,
3284-
},
3285-
Message: "<msg>",
3286-
}
3287-
if got, want := r.Error(), "PUT https://example.com: 429 <msg>"; got != want {
3288-
t.Errorf("AbuseRateLimitError = %q, want %q", got, want)
3289-
}
3280+
t.Run("nil RetryAfter", func(t *testing.T) {
3281+
t.Parallel()
3282+
r := &AbuseRateLimitError{
3283+
Response: &http.Response{
3284+
Request: &http.Request{Method: "PUT", URL: u},
3285+
StatusCode: http.StatusTooManyRequests,
3286+
},
3287+
Message: "<msg>",
3288+
}
3289+
if got, want := r.Error(), "PUT https://example.com: 429 <msg>"; got != want {
3290+
t.Errorf("AbuseRateLimitError = %q, want %q", got, want)
3291+
}
3292+
})
3293+
3294+
t.Run("with RetryAfter", func(t *testing.T) {
3295+
t.Parallel()
3296+
d := 60 * time.Second
3297+
r := &AbuseRateLimitError{
3298+
Response: &http.Response{
3299+
Request: &http.Request{Method: "GET", URL: u},
3300+
StatusCode: http.StatusForbidden,
3301+
},
3302+
Message: "rate limited",
3303+
RetryAfter: &d,
3304+
}
3305+
if got, want := r.Error(), "GET https://example.com: 403 rate limited [retry after 1m0s]"; got != want {
3306+
t.Errorf("AbuseRateLimitError = %q, want %q", got, want)
3307+
}
3308+
})
3309+
3310+
t.Run("zero RetryAfter", func(t *testing.T) {
3311+
t.Parallel()
3312+
d := 0 * time.Second
3313+
r := &AbuseRateLimitError{
3314+
Response: &http.Response{
3315+
Request: &http.Request{Method: "POST", URL: u},
3316+
StatusCode: http.StatusForbidden,
3317+
},
3318+
Message: "rate limited",
3319+
RetryAfter: &d,
3320+
}
3321+
if got, want := r.Error(), "POST https://example.com: 403 rate limited"; got != want {
3322+
t.Errorf("AbuseRateLimitError = %q, want %q", got, want)
3323+
}
3324+
})
32903325
}
32913326

32923327
func TestBareDo_returnsOpenBody(t *testing.T) {

0 commit comments

Comments
 (0)