-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patherrors.go
45 lines (41 loc) · 1.04 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
package pinentry
import (
"errors"
"log/slog"
)
// combineErrors 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 combineErrors(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...)
}
}
// combineErrorFunc combines the error pointed to by errp with the result of
// calling f.
func combineErrorFunc(errp *error, f func() error) {
if err := f(); err != nil {
*errp = combineErrors(*errp, err)
}
}
func logErrorOrInfo(logger *slog.Logger, msg string, err error, args ...any) {
switch {
case logger == nil:
case err != nil:
logger.Error(msg, append([]any{"err", err}, args...)...)
default:
logger.Info(msg, args...)
}
}