Skip to content

fix: Include RetryAfter in AbuseRateLimitError.Error output#4181

Merged
gmlewis merged 1 commit intogoogle:masterfrom
danmoseley:go-github-abuseratelimit
Apr 26, 2026
Merged

fix: Include RetryAfter in AbuseRateLimitError.Error output#4181
gmlewis merged 1 commit intogoogle:masterfrom
danmoseley:go-github-abuseratelimit

Conversation

@danmoseley
Copy link
Copy Markdown
Contributor

When AbuseRateLimitError includes a populated RetryAfter field (from the Retry-After HTTP header), the .Error() string silently drops it:

GET https://api.github.com/search/code: 403 You have exceeded a secondary rate limit. Please retry your request again later.

This is inconsistent with RateLimitError.Error(), which already includes timing via formatRateReset:

GET https://api.github.com/search/code: 403 API rate limit exceeded. [rate reset in 2m59s]

Why this matters now

This gap has existed since 2016, but was minor because Go callers can use errors.As to inspect .RetryAfter directly.

It has become more impactful with AI coding agents. The github-mcp-server exposes GitHub API operations as MCP tools, and AI agents receive tool results as plain strings — there is no way for the model to inspect the underlying Go struct. When an agent hits a secondary rate limit, it sees no retry duration and must choose between retrying immediately (worsening the situation) or waiting an arbitrary amount of time.

Including the duration in .Error() gives agent frameworks and models the information they need to back off correctly.

After this change

GET https://api.github.com/search/code: 403 You have exceeded a secondary rate limit. Please retry your request again later. [retry after 1m0s]

When RetryAfter is nil or zero, the output is unchanged.

Fixes #4180.

Note

This PR was authored with the assistance of GitHub Copilot.

@google-cla
Copy link
Copy Markdown

google-cla Bot commented Apr 25, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@danmoseley
Copy link
Copy Markdown
Contributor Author

@gmlewis

@danmoseley danmoseley force-pushed the go-github-abuseratelimit branch 2 times, most recently from 48bab6a to 6bf9f02 Compare April 25, 2026 21:29
@danmoseley
Copy link
Copy Markdown
Contributor Author

hmm, trying to figure out CLA magic

@danmoseley danmoseley force-pushed the go-github-abuseratelimit branch from 6bf9f02 to 4ceebec Compare April 25, 2026 21:33
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.
@danmoseley danmoseley force-pushed the go-github-abuseratelimit branch from 4ceebec to ca9426b Compare April 25, 2026 21:33
@danmoseley
Copy link
Copy Markdown
Contributor Author

OK CLA is good. Ready for review.

@danmoseley
Copy link
Copy Markdown
Contributor Author

BTW @gmlewis I wonder whther it might be good in the repo settings to enable copilot code review agent on PR's ? I've gotten used to it. Thats if you didn't try it already and found insufficient value.

@gmlewis
Copy link
Copy Markdown
Collaborator

gmlewis commented Apr 25, 2026

BTW @gmlewis I wonder whther it might be good in the repo settings to enable copilot code review agent on PR's ? I've gotten used to it. Thats if you didn't try it already and found insufficient value.

I no longer have authorization to modify permissions settings of this repo, @danmoseley.
If the Google Open Source team decides to turn it on, then we will have it available.
Otherwise, people have been using their own access to agents to work on PRs.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 25, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.69%. Comparing base (f019b7f) to head (ca9426b).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #4181   +/-   ##
=======================================
  Coverage   93.69%   93.69%           
=======================================
  Files         210      210           
  Lines       19760    19763    +3     
=======================================
+ Hits        18514    18517    +3     
  Misses       1049     1049           
  Partials      197      197           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@gmlewis gmlewis changed the title Include RetryAfter in AbuseRateLimitError.Error() output fix: Include RetryAfter in AbuseRateLimitError.Error output Apr 25, 2026
@gmlewis gmlewis added the NeedsReview PR is awaiting a review before merging. label Apr 25, 2026
Copy link
Copy Markdown
Collaborator

@gmlewis gmlewis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, @danmoseley!
LGTM.
Awaiting second LGTM+Approval from any other contributor to this repo before merging.

cc: @stevehipwell - @alexandear - @zyfy29 - @Not-Dhananjay-Mishra - @munlicode

Copy link
Copy Markdown
Contributor

@Not-Dhananjay-Mishra Not-Dhananjay-Mishra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@gmlewis gmlewis removed the NeedsReview PR is awaiting a review before merging. label Apr 26, 2026
@gmlewis
Copy link
Copy Markdown
Collaborator

gmlewis commented Apr 26, 2026

Thank you, @Not-Dhananjay-Mishra!
Merging.

@gmlewis gmlewis merged commit a75818a into google:master Apr 26, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AbuseRateLimitError.Error() silently drops RetryAfter field

3 participants