forked from viamrobotics/goutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
72 lines (64 loc) · 2.01 KB
/
error.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
package utils
import (
"strings"
"github.com/edaniels/golog"
"github.com/pkg/errors"
"go.uber.org/multierr"
"go.uber.org/zap"
)
// FilterOutError filters out an error based on the given target. For
// example, if err was context.Canceled and so was the target, this
// would return nil. Furthermore, if err was a multierr containing
// a context.Canceled, it would also be filtered out from a new
// multierr.
func FilterOutError(err, target error) error {
if err == nil {
return nil
}
if target == nil {
return err
}
errs := multierr.Errors(err)
if len(errs) == 1 {
// multierr flattens errors so we can assume this
// is not a multierr
if errors.Is(err, target) || strings.Contains(err.Error(), target.Error()) {
return nil
}
return err
}
newErrs := make([]error, 0, len(errs))
for _, e := range errs {
if FilterOutError(e, target) == nil {
continue
}
newErrs = append(newErrs, e)
}
return multierr.Combine(newErrs...)
}
// NewConfigValidationError returns a config validation error
// occurring at a given path.
func NewConfigValidationError(path string, err error) error {
return errors.Wrapf(err, "error validating %q", path)
}
// NewConfigValidationFieldRequiredError returns a config validation
// error for a field missing at a given path.
func NewConfigValidationFieldRequiredError(path, field string) error {
return NewConfigValidationError(path, errors.Errorf("%q is required", field))
}
// UncheckedError is used in places where we really do not care about an error but we
// want to at least report it. Never use this for closing writers.
func UncheckedError(err error) {
uncheckedError(err)
}
func uncheckedError(err error) {
if err == nil {
return
}
golog.Global().Desugar().WithOptions(zap.AddCallerSkip(2)).Sugar().Debugw("unchecked error", "error", err)
}
// UncheckedErrorFunc is used in places where we really do not care about an error but we
// want to at least report it. Never use this for closing writers.
func UncheckedErrorFunc(f func() error) {
uncheckedError(f())
}