-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
42 lines (36 loc) · 954 Bytes
/
errors.go
File metadata and controls
42 lines (36 loc) · 954 Bytes
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
package d1http
import (
"errors"
"fmt"
"net/http"
)
type APIError struct {
StatusCode int
Errors []ResponseInfo
Messages []ResponseInfo
Body string
}
func (e *APIError) Error() string {
if len(e.Errors) > 0 {
return fmt.Sprintf("cloudflare api error %d: %s", e.StatusCode, e.Errors[0].Message)
}
if e.Body != "" {
return fmt.Sprintf("cloudflare api error %d: %s", e.StatusCode, e.Body)
}
return fmt.Sprintf("cloudflare api error %d", e.StatusCode)
}
func IsRateLimited(err error) bool {
var apiErr *APIError
return errors.As(err, &apiErr) && apiErr.StatusCode == http.StatusTooManyRequests
}
func IsNotFound(err error) bool {
var apiErr *APIError
return errors.As(err, &apiErr) && apiErr.StatusCode == http.StatusNotFound
}
func IsRetryable(err error) bool {
var apiErr *APIError
if !errors.As(err, &apiErr) {
return false
}
return apiErr.StatusCode == http.StatusTooManyRequests || apiErr.StatusCode >= 500
}