-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
51 lines (45 loc) · 1.36 KB
/
Copy patherrors.go
File metadata and controls
51 lines (45 loc) · 1.36 KB
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
package leadsdb
import (
"errors"
"fmt"
"net/http"
)
// Sentinel errors for common API error cases.
var (
ErrNotFound = errors.New("leadsdb: not found")
ErrUnauthorized = errors.New("leadsdb: unauthorized")
ErrRateLimited = errors.New("leadsdb: rate limited")
ErrForbidden = errors.New("leadsdb: forbidden")
ErrInternal = errors.New("leadsdb: internal server error")
)
// APIError represents an error response from the LeadsDB API.
type APIError struct {
StatusCode int `json:"-"`
Code string `json:"code"`
Message string `json:"message"`
RetryAfter int `json:"-"` // seconds, from Retry-After header (for 429)
}
// Error implements the error interface.
func (e *APIError) Error() string {
if e.Code != "" {
return fmt.Sprintf("leadsdb: %s: %s (status %d)", e.Code, e.Message, e.StatusCode)
}
return fmt.Sprintf("leadsdb: %s (status %d)", e.Message, e.StatusCode)
}
// Is implements errors.Is support for sentinel errors.
func (e *APIError) Is(target error) bool {
switch e.StatusCode {
case http.StatusUnauthorized:
return target == ErrUnauthorized
case http.StatusForbidden:
return target == ErrForbidden
case http.StatusNotFound:
return target == ErrNotFound
case http.StatusTooManyRequests:
return target == ErrRateLimited
case http.StatusInternalServerError:
return target == ErrInternal
default:
return false
}
}