diff options
author | Ian Lance Taylor <iant@golang.org> | 2020-01-02 15:05:27 -0800 |
---|---|---|
committer | Ian Lance Taylor <iant@golang.org> | 2020-01-21 23:53:22 -0800 |
commit | 5a8ea165926cb0737ab03bc48c18dc5198ab5305 (patch) | |
tree | 962dc3357c57f019f85658f99e2e753e30201c27 /libgo/go/errors | |
parent | 6ac6529e155c9baa0aaaed7aca06bd38ebda5b43 (diff) | |
download | gcc-5a8ea165926cb0737ab03bc48c18dc5198ab5305.tar.gz |
libgo: update to Go1.14beta1
Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/214297
Diffstat (limited to 'libgo/go/errors')
-rw-r--r-- | libgo/go/errors/errors.go | 8 | ||||
-rw-r--r-- | libgo/go/errors/wrap.go | 19 |
2 files changed, 19 insertions, 8 deletions
diff --git a/libgo/go/errors/errors.go b/libgo/go/errors/errors.go index 85d4260762e..d923ad4b703 100644 --- a/libgo/go/errors/errors.go +++ b/libgo/go/errors/errors.go @@ -13,16 +13,16 @@ // // If e.Unwrap() returns a non-nil error w, then we say that e wraps w. // +// Unwrap unpacks wrapped errors. If its argument's type has an +// Unwrap method, it calls the method once. Otherwise, it returns nil. +// // A simple way to create wrapped errors is to call fmt.Errorf and apply the %w verb // to the error argument: // -// fmt.Errorf("... %w ...", ..., err, ...).Unwrap() +// errors.Unwrap(fmt.Errorf("... %w ...", ..., err, ...)) // // returns err. // -// Unwrap unpacks wrapped errors. If its argument's type has an -// Unwrap method, it calls the method once. Otherwise, it returns nil. -// // Is unwraps its first argument sequentially looking for an error that matches the // second. It reports whether it finds a match. It should be used in preference to // simple equality checks: 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") |