-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
287 additions
and
305 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package errors | ||
|
||
import "reflect" | ||
|
||
// 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 interface{}) bool { | ||
if target == nil { | ||
panic("errors: target cannot be nil") | ||
} | ||
val := reflect.ValueOf(target) | ||
typ := val.Type() | ||
if typ.Kind() != reflect.Ptr || val.IsNil() { | ||
panic("errors: target must be a non-nil pointer") | ||
} | ||
if e := typ.Elem(); e.Kind() != reflect.Interface && !e.Implements(errorType) { | ||
panic("errors: *target must be interface or implement error") | ||
} | ||
targetType := typ.Elem() | ||
for err != nil { | ||
if reflect.TypeOf(err).AssignableTo(targetType) { | ||
val.Elem().Set(reflect.ValueOf(err)) | ||
return true | ||
} | ||
if x, ok := err.(interface{ As(interface{}) bool }); ok && x.As(target) { | ||
return true | ||
} | ||
err = Unwrap(err) | ||
} | ||
return false | ||
} | ||
|
||
// AsSlice tests err.As for errs slice | ||
func AsSlice(errs []error, target interface{}) bool { | ||
if target == nil { | ||
panic("errors: target cannot be nil") | ||
} | ||
val := reflect.ValueOf(target) | ||
typ := val.Type() | ||
if typ.Kind() != reflect.Ptr || val.IsNil() { | ||
panic("errors: target must be a non-nil pointer") | ||
} | ||
if e := typ.Elem(); e.Kind() != reflect.Interface && !e.Implements(errorType) { | ||
panic("errors: *target must be interface or implement error") | ||
} | ||
targetType := typ.Elem() | ||
for _, err := range errs { | ||
if reflect.TypeOf(err).AssignableTo(targetType) { | ||
val.Elem().Set(reflect.ValueOf(err)) | ||
return true | ||
} | ||
if x, ok := err.(interface{ As(interface{}) bool }); ok && x.As(target) { | ||
return true | ||
} | ||
err = Unwrap(err) | ||
} | ||
return false | ||
} | ||
|
||
var errorType = reflect.TypeOf((*error)(nil)).Elem() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.