Skip to content

Commit c3f5683

Browse files
dmitshurwillnorris
authored andcommitted
Add RateLimitError type, detect and return it when appropriate.
Fixes #152. Add tests for RateLimitError. Refactor populateRate into a more general parseRate.
1 parent 1219390 commit c3f5683

3 files changed

Lines changed: 89 additions & 10 deletions

File tree

‎github/doc.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ been some time since the last API call and other clients have made subsequent
6565
requests since then. You can always call RateLimits() directly to get the most
6666
up-to-date rate limit data for the client.
6767
68+
To detect an API rate limit error, you can check if its type is *github.RateLimitError:
69+
70+
repos, _, err := client.Repositories.List("", nil)
71+
if _, ok := err.(*github.RateLimitError); ok {
72+
log.Println("hit rate limit")
73+
}
74+
6875
Learn more about GitHub rate limiting at
6976
http://developer.github.com/v3/#rate-limiting.
7077

‎github/github.go‎

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ type Response struct {
236236
func newResponse(r *http.Response) *Response {
237237
response := &Response{Response: r}
238238
response.populatePageValues()
239-
response.populateRate()
239+
response.Rate = parseRate(r)
240240
return response
241241
}
242242

@@ -284,19 +284,21 @@ func (r *Response) populatePageValues() {
284284
}
285285
}
286286

287-
// populateRate parses the rate related headers and populates the response Rate.
288-
func (r *Response) populateRate() {
287+
// parseRate parses the rate related headers.
288+
func parseRate(r *http.Response) Rate {
289+
var rate Rate
289290
if limit := r.Header.Get(headerRateLimit); limit != "" {
290-
r.Rate.Limit, _ = strconv.Atoi(limit)
291+
rate.Limit, _ = strconv.Atoi(limit)
291292
}
292293
if remaining := r.Header.Get(headerRateRemaining); remaining != "" {
293-
r.Rate.Remaining, _ = strconv.Atoi(remaining)
294+
rate.Remaining, _ = strconv.Atoi(remaining)
294295
}
295296
if reset := r.Header.Get(headerRateReset); reset != "" {
296297
if v, _ := strconv.ParseInt(reset, 10, 64); v != 0 {
297-
r.Rate.Reset = Timestamp{time.Unix(v, 0)}
298+
rate.Reset = Timestamp{time.Unix(v, 0)}
298299
}
299300
}
301+
return rate
300302
}
301303

302304
// Rate specifies the current rate limit for the client as determined by the
@@ -373,6 +375,20 @@ type TwoFactorAuthError ErrorResponse
373375

374376
func (r *TwoFactorAuthError) Error() string { return (*ErrorResponse)(r).Error() }
375377

378+
// RateLimitError occurs when GitHub returns 403 Forbidden response with a rate limit
379+
// remaining value of 0, and error message starts with "API rate limit exceeded for ".
380+
type RateLimitError struct {
381+
Rate Rate // Rate specifies last known rate limit for the client
382+
Response *http.Response // HTTP response that caused this error
383+
Message string `json:"message"` // error message
384+
}
385+
386+
func (r *RateLimitError) Error() string {
387+
return fmt.Sprintf("%v %v: %d %v; rate reset in %v",
388+
r.Response.Request.Method, sanitizeURL(r.Response.Request.URL),
389+
r.Response.StatusCode, r.Message, r.Rate.Reset.Time.Sub(time.Now()))
390+
}
391+
376392
// sanitizeURL redacts the client_secret parameter from the URL which may be
377393
// exposed to the user, specifically in the ErrorResponse error message.
378394
func sanitizeURL(uri *url.URL) *url.URL {
@@ -427,10 +443,18 @@ func CheckResponse(r *http.Response) error {
427443
if err == nil && data != nil {
428444
json.Unmarshal(data, errorResponse)
429445
}
430-
if r.StatusCode == http.StatusUnauthorized && strings.HasPrefix(r.Header.Get(headerOTP), "required") {
446+
switch {
447+
case r.StatusCode == http.StatusUnauthorized && strings.HasPrefix(r.Header.Get(headerOTP), "required"):
431448
return (*TwoFactorAuthError)(errorResponse)
449+
case r.StatusCode == http.StatusForbidden && r.Header.Get(headerRateRemaining) == "0" && strings.HasPrefix(errorResponse.Message, "API rate limit exceeded for "):
450+
return &RateLimitError{
451+
Rate: parseRate(r),
452+
Response: errorResponse.Response,
453+
Message: errorResponse.Message,
454+
}
455+
default:
456+
return errorResponse
432457
}
433-
return errorResponse
434458
}
435459

436460
// parseBoolResponse determines the boolean result from a GitHub API response.

‎github/github_test.go‎

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,11 @@ func TestDo_rateLimit(t *testing.T) {
389389
}
390390

391391
req, _ := client.NewRequest("GET", "/", nil)
392-
client.Do(req, nil)
392+
_, err := client.Do(req, nil)
393393

394+
if err != nil {
395+
t.Errorf("Do returned unexpected error: %v", err)
396+
}
394397
if got, want := client.Rate().Limit, 60; got != want {
395398
t.Errorf("Client rate limit = %v, want %v", got, want)
396399
}
@@ -416,8 +419,14 @@ func TestDo_rateLimit_errorResponse(t *testing.T) {
416419
})
417420

418421
req, _ := client.NewRequest("GET", "/", nil)
419-
client.Do(req, nil)
422+
_, err := client.Do(req, nil)
420423

424+
if err == nil {
425+
t.Error("Expected error to be returned.")
426+
}
427+
if _, ok := err.(*RateLimitError); ok {
428+
t.Errorf("Did not expect a *RateLimitError error; got %#v.", err)
429+
}
421430
if got, want := client.Rate().Limit, 60; got != want {
422431
t.Errorf("Client rate limit = %v, want %v", got, want)
423432
}
@@ -430,6 +439,45 @@ func TestDo_rateLimit_errorResponse(t *testing.T) {
430439
}
431440
}
432441

442+
// Ensure *RateLimitError is returned when API rate limit is exceeded.
443+
func TestDo_rateLimit_rateLimitError(t *testing.T) {
444+
setup()
445+
defer teardown()
446+
447+
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
448+
w.Header().Add(headerRateLimit, "60")
449+
w.Header().Add(headerRateRemaining, "0")
450+
w.Header().Add(headerRateReset, "1372700873")
451+
w.Header().Set("Content-Type", "application/json; charset=utf-8")
452+
w.WriteHeader(http.StatusForbidden)
453+
fmt.Fprintln(w, `{
454+
"message": "API rate limit exceeded for xxx.xxx.xxx.xxx. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)",
455+
"documentation_url": "https://developer.github.com/v3/#rate-limiting"
456+
}`)
457+
})
458+
459+
req, _ := client.NewRequest("GET", "/", nil)
460+
_, err := client.Do(req, nil)
461+
462+
if err == nil {
463+
t.Error("Expected error to be returned.")
464+
}
465+
rateLimitErr, ok := err.(*RateLimitError)
466+
if !ok {
467+
t.Fatalf("Expected a *RateLimitError error; got %#v.", err)
468+
}
469+
if got, want := rateLimitErr.Rate.Limit, 60; got != want {
470+
t.Errorf("rateLimitErr rate limit = %v, want %v", got, want)
471+
}
472+
if got, want := rateLimitErr.Rate.Remaining, 0; got != want {
473+
t.Errorf("rateLimitErr rate remaining = %v, want %v", got, want)
474+
}
475+
reset := time.Date(2013, 7, 1, 17, 47, 53, 0, time.UTC)
476+
if rateLimitErr.Rate.Reset.UTC() != reset {
477+
t.Errorf("rateLimitErr rate reset = %v, want %v", client.Rate().Reset, reset)
478+
}
479+
}
480+
433481
func TestDo_noContent(t *testing.T) {
434482
setup()
435483
defer teardown()

0 commit comments

Comments
 (0)