diff options
Diffstat (limited to 'libgo/go/errors/wrap.go')
-rw-r--r-- | libgo/go/errors/wrap.go | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/libgo/go/errors/wrap.go b/libgo/go/errors/wrap.go index 240da37c295..272d056b318 100644 --- a/libgo/go/errors/wrap.go +++ b/libgo/go/errors/wrap.go @@ -28,6 +28,14 @@ func Unwrap(err error) error { // // An error is considered to match a target if it is equal to that target or if // it implements a method Is(error) bool such that Is(target) returns true. +// +// An error type might provide an Is method so it can be treated as equivalent +// to an existing error. For example, if MyError defines +// +// func (m MyError) Is(target error) bool { return target == os.ErrExist } +// +// then Is(MyError{}, os.ErrExist) returns true. See syscall.Errno.Is for +// an example in the standard library. func Is(err, target error) bool { if target == nil { return err == target @@ -41,7 +49,7 @@ func Is(err, target error) bool { if x, ok := err.(interface{ Is(error) bool }); ok && x.Is(target) { return true } - // TODO: consider supporing target.Is(err). This would allow + // TODO: consider supporting target.Is(err). This would allow // user-definable predicates, but also may allow for coping with sloppy // APIs, thereby making it easier to get away with them. if err = Unwrap(err); err == nil { @@ -51,7 +59,7 @@ func Is(err, target error) bool { } // As finds the first error in err's chain that matches target, and if so, sets -// target to that error value and returns true. +// target to that error value and returns true. Otherwise, it returns false. // // The chain consists of err itself followed by the sequence of errors obtained by // repeatedly calling Unwrap. @@ -61,8 +69,11 @@ func Is(err, target error) bool { // As(target) returns true. In the latter case, the As method is responsible for // setting target. // -// As will panic if target is not a non-nil pointer to either a type that implements -// error, or to any interface type. As returns false if err is nil. +// An error type might provide an As method so it can be treated as if it were a +// a different error type. +// +// As panics if target is not a non-nil pointer to either a type that implements +// error, or to any interface type. func As(err error, target interface{}) bool { if target == nil { panic("errors: target cannot be nil") |