-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
58 lines (52 loc) · 1.08 KB
/
utils.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
package e5
import (
"errors"
"io"
"strings"
)
// Close returns a WrapFunc that closes the Closer
func Close(c io.Closer) WrapFunc {
return func(prev error) error {
if err := c.Close(); err != nil {
return With(err)(prev)
}
return prev
}
}
// Do returns a WrapFunc that calls fn
func Do(fn func()) WrapFunc {
return func(prev error) error {
fn()
return prev
}
}
// Ignore returns a WrapFunc that returns nil if errors.Is(prev, err) is true
func Ignore(err error) WrapFunc {
return func(prev error) error {
if errors.Is(prev, err) {
return nil
}
return prev
}
}
// IgnoreAs returns a WrapFunc that returns nil if errors.As(prev, target) is true
func IgnoreAs(target any) WrapFunc {
return func(prev error) error {
if errors.As(prev, target) {
return nil
}
return prev
}
}
// IgnoreContains returns a WrapFunc that returns nil if prev.Error() contains str
func IgnoreContains(str string) WrapFunc {
return func(prev error) error {
if prev == nil {
return nil
}
if e := prev.Error(); strings.Contains(e, str) {
return nil
}
return prev
}
}