-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherr.go
129 lines (102 loc) · 2.01 KB
/
err.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package apigo
import (
"errors"
"fmt"
"net/http"
)
type Err struct {
Code int
Cause string
Err error
}
func (o *Err) Is(target error) bool {
if t, ok := target.(*Err); ok {
return t.Code == o.Code
}
return false
}
func (o *Err) Unwrap() error {
return o.Err
}
func (o *Err) Error() string {
return fmt.Sprintf("%d; %s", o.Code, o.Cause)
}
func errIs(err error, httpStatus int) bool {
var webErr *Err
if errors.As(err, &webErr) {
if webErr.Code == httpStatus {
return true
}
}
return false
}
// 400: BadRequest
func ErrIsBadRequest(err error) bool {
return errIs(err, http.StatusBadRequest)
}
func ErrBadRequest(cause string) *Err {
return &Err{
Code: http.StatusBadRequest,
Cause: cause,
}
}
// 404: NotFound
func ErrIsNotFound(err error) bool {
return errIs(err, http.StatusNotFound)
}
func ErrNotFound(cause string) *Err {
return &Err{
Code: http.StatusNotFound,
Cause: cause,
}
}
// 409: Conflict
func ErrIsConflict(err error) bool {
return errIs(err, http.StatusConflict)
}
func ErrConflict(cause string) *Err {
return &Err{
Code: http.StatusConflict,
Cause: cause,
}
}
// 422: UnprocessableEntity
func ErrIsUnprocessableEntity(err error) bool {
return errIs(err, http.StatusUnprocessableEntity)
}
func ErrUnprocessableEntity(cause string) *Err {
return &Err{
Code: http.StatusUnprocessableEntity,
Cause: cause,
}
}
// 429: TooManyRequests
func ErrIsTooManyRequests(err error) bool {
return errIs(err, http.StatusTooManyRequests)
}
func ErrTooManyRequests(cause string) *Err {
return &Err{
Code: http.StatusTooManyRequests,
Cause: cause,
}
}
// 401: Unauthorized
func ErrIsUnauthorized(err error) bool {
return errIs(err, http.StatusUnauthorized)
}
func ErrUnauthorized(cause string) *Err {
return &Err{
Code: http.StatusUnauthorized,
Cause: cause,
}
}
// 403; Forbidden
func ErrIsForbidden(err error) bool {
return errIs(err, http.StatusForbidden)
}
func ErrForbidden(cause string) *Err {
return &Err{
Code: http.StatusForbidden,
Cause: cause,
}
}