-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
86 lines (74 loc) · 1.39 KB
/
errors.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
package errors
import "errors"
type Option func(err error) error
// New is an alias for the default `errors.New`.
var New = errors.New
func Http(status int) Option {
return func(reason error) error {
return WrapHttp(reason, status)
}
}
func Module(module string) Option {
return func(reason error) error {
return WrapModule(reason, module)
}
}
func Code(code string) Option {
return func(reason error) error {
return WrapCode(reason, code)
}
}
func Validation() Option {
return func(err error) error {
return WrapValidation(err)
}
}
func Message(message string) Option {
return func(err error) error {
return WrapMessage(err, message)
}
}
func Combine(options ...interface{}) Option {
return func(err error) error {
for _, opt := range options {
switch act := opt.(type) {
case Option:
err = act(err)
case string:
err = WrapMessage(err, act)
case int:
err = WrapHttp(err, act)
default:
continue
}
}
return err
}
}
func Wrap(reason error, options ...interface{}) error {
return Combine(options...)(reason)
}
func Is(err, target error) bool {
for {
if err == target {
return true
}
wrapper, ok := err.(Wrapper)
if !ok {
return false
}
err = wrapper.Unwrap()
if err == nil {
return false
}
}
}
func Reason(err error) error {
for {
wrapper, ok := err.(Wrapper)
if !ok {
return err
}
err = wrapper.Unwrap()
}
}