-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
54 lines (45 loc) · 1.58 KB
/
Copy patherrors.go
File metadata and controls
54 lines (45 loc) · 1.58 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
52
53
54
package squad
import (
"errors"
"fmt"
)
// Error represents a Squad API error response.
// It wraps both the HTTP-level status and Squad's application-level status code.
type Error struct {
HTTPStatus int
Status int
Message string
Code string
}
func (e *Error) Error() string {
return fmt.Sprintf("squad: status %d: %s (http=%d)", e.Status, e.Message, e.HTTPStatus)
}
// Is allows errors.Is comparisons by HTTPStatus and Status fields, not pointer identity.
func (e *Error) Is(target error) bool {
var t *Error
if errors.As(target, &t) {
return e.HTTPStatus == t.HTTPStatus && e.Status == t.Status
}
return false
}
// Sentinel errors for common API failure modes.
var (
ErrUnauthorized = &Error{HTTPStatus: 401, Status: 401, Message: "unauthorized"}
ErrForbidden = &Error{HTTPStatus: 403, Status: 403, Message: "forbidden"}
ErrBadRequest = &Error{HTTPStatus: 400, Status: 400, Message: "bad request"}
ErrNotFound = &Error{HTTPStatus: 404, Status: 404, Message: "not found"}
)
// ErrInvalidSignature is returned by ParseWebhook when HMAC-SHA512 validation fails.
var ErrInvalidSignature = fmt.Errorf("squad: webhook signature validation failed")
// IsUnauthorized reports whether err is a 401 authorization failure.
func IsUnauthorized(err error) bool {
return errors.Is(err, ErrUnauthorized)
}
// IsBadRequest reports whether err is a 400 validation failure.
func IsBadRequest(err error) bool {
return errors.Is(err, ErrBadRequest)
}
// IsNotFound reports whether err is a 404 not-found failure.
func IsNotFound(err error) bool {
return errors.Is(err, ErrNotFound)
}