-
Notifications
You must be signed in to change notification settings - Fork 72
/
errors.go
59 lines (47 loc) · 1.44 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package trello
import (
"fmt"
"io/ioutil"
"net/http"
)
type notFoundError interface {
IsNotFound() bool
}
type rateLimitError interface {
IsRateLimit() bool
}
type permissionDeniedError interface {
IsPermissionDenied() bool
}
type httpClientError struct {
msg string
code int
}
func makeHTTPClientError(url string, resp *http.Response) error {
body, _ := ioutil.ReadAll(resp.Body)
msg := fmt.Sprintf("HTTP request failure on %s:\n%d: %s", url, resp.StatusCode, string(body))
return &httpClientError{
msg: msg,
code: resp.StatusCode,
}
}
func (e *httpClientError) Error() string { return e.msg }
func (e *httpClientError) IsRateLimit() bool { return e.code == 429 }
func (e *httpClientError) IsNotFound() bool { return e.code == 404 }
func (e *httpClientError) IsPermissionDenied() bool { return e.code == 401 }
// IsRateLimit takes an error and returns true exactly if the error is a rate-limit error.
func IsRateLimit(err error) bool {
re, ok := err.(rateLimitError)
return ok && re.IsRateLimit()
}
// IsNotFound takes an error and returns true exactly if the error is a not-found error.
func IsNotFound(err error) bool {
nf, ok := err.(notFoundError)
return ok && nf.IsNotFound()
}
// IsPermissionDenied takes an error and returns true exactly if the error is a
// permission-denied error.
func IsPermissionDenied(err error) bool {
pd, ok := err.(permissionDeniedError)
return ok && pd.IsPermissionDenied()
}