errors: Make Cause() unwrap *url.Error

This commit is contained in:
Alexander Neumann 2018-01-23 21:40:51 +01:00
parent 685ce719ad
commit 8e812b7ac0
1 changed files with 30 additions and 5 deletions

View File

@ -1,11 +1,10 @@
package errors
import "github.com/pkg/errors"
import (
"net/url"
// Cause returns the cause of an error.
func Cause(err error) error {
return errors.Cause(err)
}
"github.com/pkg/errors"
)
// New creates a new error based on message. Wrapped so that this package does
// not appear in the stack trace.
@ -22,3 +21,29 @@ var Wrap = errors.Wrap
// Wrapf returns an error annotating err with the format specifier. If err is
// nil, Wrapf returns nil.
var Wrapf = errors.Wrapf
// Cause returns the cause of an error. It will also unwrap certain errors,
// e.g. *url.Error returned by the net/http client.
func Cause(err error) error {
type Causer interface {
Cause() error
}
for {
// unwrap *url.Error
if urlErr, ok := err.(*url.Error); ok {
err = urlErr.Err
continue
}
// if err is a Causer, return the cause for this error.
if c, ok := err.(Causer); ok {
err = c.Cause()
continue
}
break
}
return err
}