forked from jjeffery/errors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cause.go
37 lines (34 loc) · 885 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
37
package errors
// Cause was copied from https://github.com/pkg/errors
// for compatibility. See CREDITS.md.
// Cause returns the underlying cause of the error, if possible.
// An error value has a cause if it implements the following
// interface:
//
// type causer interface {
// Cause() error
// }
//
// If the error does not implement Cause, the original error will
// be returned. If the error is nil, nil will be returned without further
// investigation.
//
// Cause is compatible with the Cause function in package "github.com/pkg/errors".
// The implementation and documentation of Cause has been copied from that package.
func Cause(err error) error {
type causer interface {
Cause() error
}
for err != nil {
cause, ok := err.(causer)
if !ok {
break
}
if e := cause.Cause(); e != nil {
err = e
} else {
break
}
}
return err
}