Skip to content

Commit 24e5434

Browse files
committed
rate limit error code
1 parent 36783c3 commit 24e5434

3 files changed

Lines changed: 23 additions & 0 deletions

File tree

‎pkg/core/core_ical.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ func (c *Core) fetchICalURL(name string, sourceURL *url.URL) error {
210210
code = errcode.Auth
211211
case http.StatusForbidden:
212212
code = errcode.Forbidden
213+
case http.StatusTooManyRequests:
214+
code = errcode.RateLimit
213215
}
214216
return errcode.Wrap(code, fmt.Errorf("fetch iCalendar: %s", response.Status))
215217
}

‎pkg/core/git.go‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package core
33
import (
44
"errors"
55
"fmt"
6+
"net/http"
67
"net/url"
78
"path"
89
"time"
@@ -83,10 +84,29 @@ func wrapGitRemoteError(err error) error {
8384
code = errcode.Auth
8485
case errors.Is(err, transport.ErrAuthorizationFailed):
8586
code = errcode.Forbidden
87+
case gitHTTPStatusCode(err) == http.StatusTooManyRequests:
88+
code = errcode.RateLimit
8689
}
8790
return errcode.Wrap(code, err)
8891
}
8992

93+
func gitHTTPStatusCode(err error) int {
94+
type statusError interface {
95+
StatusCode() int
96+
}
97+
98+
var statusErr statusError
99+
if errors.As(err, &statusErr) {
100+
return statusErr.StatusCode()
101+
}
102+
103+
var unexpectedErr *plumbing.UnexpectedError
104+
if errors.As(err, &unexpectedErr) && errors.As(unexpectedErr.Err, &statusErr) {
105+
return statusErr.StatusCode()
106+
}
107+
return 0
108+
}
109+
90110
// ignoreUpToDate swallows the benign "already up to date" error from push/fetch.
91111
func ignoreUpToDate(err error) error {
92112
if errors.Is(err, gogit.NoErrAlreadyUpToDate) {

‎pkg/errcode/errcode.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const (
1010
Network Code = "NETWORK"
1111
Auth Code = "AUTH"
1212
Forbidden Code = "FORBIDDEN"
13+
RateLimit Code = "RATELIMIT"
1314
Validation Code = "VALIDATION"
1415
Storage Code = "STORAGE"
1516
)

0 commit comments

Comments
 (0)