Open
Description
Let's imagine we are using a mix of this library and standard errors created with errors.New
or fmt.Errorf
.
Consider the following snippet:
var errStd = stderrors.New("something")
var errKind = errors.NewKind("something: %s")
var myErr = errKind.Wrap(errStd, "foo")
If we want to check if myErr has errStd
as its cause, we need to do the following:
err, ok := myErr.(*errors.Error)
if !ok {
// handle
}
if err.Cause() == errStd {
}
This is potentially more cumbersome if the original error is deeply nested.
Perhaps we could have a function IsCausedBy(err, cause)
.
In this case, it would become:
if errors.Is(myErr, errStd) {
// do something
}
Implementation could look like this:
func Is(err, cause error) bool {
e, ok := err.(*Error)
if !ok {
return false
}
if e, ok := e.cause.(*Error); ok {
return Is(e, cause)
}
return e.cause == cause
}
Metadata
Metadata
Assignees
Labels
No labels