feat(errors/gerror): add As support (#4002)

This commit is contained in:
John Guo
2024-12-09 23:11:56 +08:00
committed by GitHub
parent 5e47590165
commit 80e73da416
3 changed files with 87 additions and 1 deletions

View File

@ -98,6 +98,23 @@ func Is(err, target error) bool {
return errors.Is(err, target)
}
// As finds the first error in err's chain that matches target, and if so, sets
// target to that error value and returns true.
//
// The chain consists of err itself followed by the sequence of errors obtained by
// repeatedly calling Unwrap.
//
// An error matches target if the error's concrete value is assignable to the value
// pointed to by target, or if the error has a method As(interface{}) bool such that
// 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.
func As(err error, target any) bool {
return errors.As(err, target)
}
// HasError performs as Is.
// This function is designed and implemented early before errors.Is of go stdlib.
// Deprecated: use Is instead.