Skip to content
This repository has been archived by the owner on Dec 1, 2021. It is now read-only.

Intelligent errors.Cause() #143

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@
// }
//
// can be inspected by errors.Cause. errors.Cause will recursively retrieve
// the topmost error which does not implement causer, which is assumed to be
// the original cause. For example:
// the topmost error which does not implement causer (which is assumed to be
// the original cause), or the error which returns nil or itself as the cause.
// For example:
//
// switch err := errors.Cause(err).(type) {
// case *MyError:
Expand Down Expand Up @@ -250,9 +251,8 @@ func (w *withMessage) Format(s fmt.State, verb rune) {
// 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.
// An underlying cause is one which does not implement clause,
// or one which returns nil or itself as the cause.
func Cause(err error) error {
type causer interface {
Cause() error
Expand All @@ -261,9 +261,13 @@ func Cause(err error) error {
for err != nil {
cause, ok := err.(causer)
if !ok {
break
return err
}
err = cause.Cause()
errCause := cause.Cause()
if errCause == nil || errCause == err {
return err
}
err = errCause
}
return err
}