Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ language: go

go:
- "1.8.x"
- "1.14.x"
- tip
- "1.12.x"
- "1.13.x"
- "1.15.x"
29 changes: 29 additions & 0 deletions error_is.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// +build go1.13

package errorx

import "errors"

// Is implements the helper interface for errors.Is
// It first checks if target is an *Error and uses IsOfType to check if e Is target.
// If target is not an *Error, it instead iterates through the error chain until it reaches a non-*Error error (or nil)
// and calls errors.Is on that error and target.
func (e *Error) Is(target error) bool {
// Check if target is an *Error, if so we can use IsOfType.
t := Cast(target)
if t != nil {
return e.IsOfType(t.Type())
}

// target is not an *Error, so iterate through the error chain until nil or a non-*Error error is reached and use
// errors.Is to check against that.
cause := e
for cause != nil {
next := Cast(cause.Cause())
if next == nil && cause.Cause() != nil {
return errors.Is(cause.Cause(), target)
}
cause = next
}
return false
}