/
userError.go
52 lines (42 loc) · 888 Bytes
/
userError.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
package core
import "fmt"
var ErrNotFound = NewUserErrorWithStatus(404, "item not found")
type UserError struct {
Original error
Status int
}
func (ue *UserError) Error() string {
return ue.Original.Error()
}
func (ue *UserError) Unwrap() error {
return ue.Original
}
func AsUserError(status int, err error) error {
return &UserError{
Original: err,
Status: status,
}
}
func NewUserError(format string, args ...any) error {
return NewUserErrorWithStatus(400, format, args...)
}
func NewUserErrorWithStatus(status int, format string, args ...any) error {
return &UserError{
Original: fmt.Errorf(format, args...),
Status: status,
}
}
func IsUserError(err error) bool {
if err == nil {
return false
}
_, ok := err.(*UserError)
return ok
}
func GetUserErrorStatus(err error) int {
ue, ok := err.(*UserError)
if !ok {
return 0
}
return ue.Status
}