generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
errors.go
60 lines (50 loc) · 1.47 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package errors
import (
"errors"
"strings"
"golang.org/x/exp/maps"
)
// UnwrapAll recursively unwraps all errors in err, including all intermediate errors.
//
//nolint:errorlint
func UnwrapAll(err error) []error {
out := []error{}
if inner, ok := err.(interface{ Unwrap() []error }); ok {
for _, e := range inner.Unwrap() {
out = append(out, UnwrapAll(e)...)
}
return out
}
if inner, ok := err.(interface{ Unwrap() error }); ok && inner.Unwrap() != nil {
out = append(out, UnwrapAll(inner.Unwrap())...)
}
out = append(out, err)
return out
}
// Innermost returns true if err cannot be further unwrapped.
//
//nolint:errorlint
func Innermost(err error) bool {
if err, ok := err.(interface{ Unwrap() []error }); ok && len(err.Unwrap()) > 0 {
return false
}
if err, ok := err.(interface{ Unwrap() error }); ok && err.Unwrap() != nil {
return false
}
return true
}
func Join(errs ...error) error { return errors.Join(errs...) }
func New(text string) error { return errors.New(text) }
func As(err error, target interface{}) bool { return errors.As(err, target) }
func Is(err, target error) bool { return errors.Is(err, target) }
func Unwrap(err error) error { return errors.Unwrap(err) }
// DeduplicateErrors de-duplicates equivalent errors.
func DeduplicateErrors(merr []error) []error {
set := map[string]error{}
for _, err := range merr {
for _, subErr := range UnwrapAll(err) {
set[strings.TrimSpace(subErr.Error())] = subErr
}
}
return maps.Values(set)
}