From aeb7619109b3442c7570a2c0bc030e4079e586dd Mon Sep 17 00:00:00 2001 From: Kurnia D Win Date: Wed, 15 Jan 2020 15:07:21 +0700 Subject: [PATCH] fix Cause Some error struct have `Cause() error` method indicating the cause of the error, but that method could return nil indicating that that itself is the root cause in the error chain look at this struct ```go type myError struct { message string cause error } func (m *myError) Error() string { ... } func (m *myError) Cause() error { return m.cause } func New(message string) error { return &myError{message: message} } ``` --- errors.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/errors.go b/errors.go index 161aea2..bc93026 100644 --- a/errors.go +++ b/errors.go @@ -282,7 +282,11 @@ func Cause(err error) error { if !ok { break } - err = cause.Cause() + if cerr := cause.Cause(); cerr != nil { + err = cerr + } else { + break + } } return err }