-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
42 lines (34 loc) · 981 Bytes
/
helpers.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
package errors
import "errors"
// Is is alias for builtin errors.Is. It implements such interface as OneOf and EachOf.
// It's useful to avoid importing default library.
//
// IMPORTANT! Is uses only first error from variadic argument.
func Is(err error, target ...error) bool {
if len(target) == 0 {
return err == nil
}
return errors.Is(err, target[0])
}
// As is alias for builtin errors.As. It's useful to avoid importing default library.
func As(err error, target interface{}) bool {
return errors.As(err, target)
}
// OneOf reports whether any error in err's chain matches at least one of target errors.
func OneOf(err error, target ...error) bool {
for _, e := range target {
if errors.Is(err, e) {
return true
}
}
return false
}
// EachOf reports whether any error in err's chain matches each of target errors.
func EachOf(err error, target ...error) bool {
for _, e := range target {
if !errors.Is(err, e) {
return false
}
}
return true
}