Skip to content

Commit db2b75a

Browse files
committed
feat(errors): improve rate limit error messages for AI agents
When GitHub API returns a rate limit error, surface clear retry duration to the agent instead of burying it in raw HTTP strings. For RateLimitError: show "Retry after Xs" For AbuseRateLimitError: show "Retry after Xs" when RetryAfter is set, otherwise show "Wait before retrying" Fixes #2385
1 parent 4bded57 commit db2b75a

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

pkg/errors/error.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package errors
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"net/http"
8+
"time"
79

810
"github.com/github/github-mcp-server/pkg/utils"
911
"github.com/google/go-github/v82/github"
@@ -159,6 +161,27 @@ func NewGitHubAPIErrorResponse(ctx context.Context, message string, resp *github
159161
if ctx != nil {
160162
_, _ = addGitHubAPIErrorToContext(ctx, apiErr) // Explicitly ignore error for graceful handling
161163
}
164+
165+
var rateLimitErr *github.RateLimitError
166+
if errors.As(err, &rateLimitErr) {
167+
retryIn := time.Until(rateLimitErr.Rate.Reset.Time).Round(time.Second)
168+
return utils.NewToolResultError(fmt.Sprintf(
169+
"%s: GitHub API rate limit exceeded. Retry after %v.",
170+
message, retryIn))
171+
}
172+
173+
var abuseErr *github.AbuseRateLimitError
174+
if errors.As(err, &abuseErr) {
175+
if abuseErr.RetryAfter != nil {
176+
return utils.NewToolResultError(fmt.Sprintf(
177+
"%s: GitHub secondary rate limit exceeded. Retry after %v.",
178+
message, abuseErr.RetryAfter.Round(time.Second)))
179+
}
180+
return utils.NewToolResultError(fmt.Sprintf(
181+
"%s: GitHub secondary rate limit exceeded. Wait before retrying.",
182+
message))
183+
}
184+
162185
return utils.NewToolResultErrorFromErr(message, err)
163186
}
164187

0 commit comments

Comments
 (0)