-
Notifications
You must be signed in to change notification settings - Fork 473
/
chezmoierrors.go
34 lines (31 loc) · 880 Bytes
/
chezmoierrors.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
// Package chezmoierrors contains convenience functions for combining multiple
// errors.
package chezmoierrors
import "errors"
// Combine combines all non-nil errors in errs into one. If there are no non-nil
// errors, it returns nil. If there is exactly one non-nil error then it returns
// that error. Otherwise, it returns the non-nil errors combined with
// errors.Join.
func Combine(errs ...error) error {
nonNilErrs := make([]error, 0, len(errs))
for _, err := range errs {
if err != nil {
nonNilErrs = append(nonNilErrs, err)
}
}
switch len(nonNilErrs) {
case 0:
return nil
case 1:
return nonNilErrs[0]
default:
return errors.Join(nonNilErrs...)
}
}
// CombineFunc combines the error pointed to by errp with the result of calling
// f.
func CombineFunc(errp *error, f func() error) {
if err := f(); err != nil {
*errp = Combine(*errp, err)
}
}