-
Notifications
You must be signed in to change notification settings - Fork 5
/
cause.go
36 lines (30 loc) · 803 Bytes
/
cause.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
package errs
// Cause first checks for `Unwrap` returning the result if available,
// otherwise returns the result of `Cause`.
// If neither are available returns nil.
//
// This supports both Go 1.13+ style `UnWrap` and pkg.errors style
// `Cause` chaining.
func Cause(err error) error {
u, ok := err.(interface{ Unwrap() error })
if ok {
return u.Unwrap() //nolint:wrapcheck // defeats the whole point
}
c, ok := err.(interface{ Cause() error })
if ok {
return c.Cause() //nolint:wrapcheck // defeats the whole point
}
return nil
}
// RootCause follows the error chain until Cause() and UnWrap() are not
// available or return nil.
func RootCause(err error) error {
for err != nil {
errCause := Cause(err)
if errCause == nil {
return err
}
err = errCause
}
return err
}