Skip to content

Commit

Permalink
soft fail when we get api rate limited (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
burmudar authored Jun 1, 2022
1 parent 4bd0cba commit 0cf14e0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
15 changes: 14 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,23 @@ var verbose io.Writer = os.Stderr
func main() {
if err := testableMain(os.Stdout, os.Args[1:]); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)

if isRateLimitErr(err) {
fmt.Println("Github API limit reached. Soft exiting")
} else {
os.Exit(1)
}
}
}

func isRateLimitErr(err error) bool {
if err == nil {
return false
}

return strings.Contains(err.Error(), "API rate limit exceeded")
}

func testableMain(stdout io.Writer, args []string) error {
opts, err := getOptions(stdout, args)
if err != nil {
Expand Down
28 changes: 28 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
Expand Down Expand Up @@ -643,6 +644,33 @@ func TestNotifications(t *testing.T) {
}
}

func TestIsRateLimitErr(t *testing.T) {
cases := []struct {
err error
expected bool
}{
{
err: fmt.Errorf("graphql: API rate limit exceeded for user ID 12345"),
expected: true,
}, {
err: nil,
expected: false,
}, {
err: fmt.Errorf("fake top error"),
expected: false,
}, {
err: fmt.Errorf("something something: API rate limit exceeded for user ID 12345"),
expected: true,
},
}

for _, tc := range cases {
if isRateLimitErr(tc.err) != tc.expected {
t.Errorf("expected %v got %v for %s", tc.expected, !tc.expected, tc.err)
}
}
}

// memfs is an in-memory implementation of the FS interface.
type memfs map[string]string

Expand Down

0 comments on commit 0cf14e0

Please sign in to comment.