Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EquateErrors helper #178

Merged
merged 2 commits into from
Dec 16, 2019
Merged

Add EquateErrors helper #178

merged 2 commits into from
Dec 16, 2019

Conversation

dsnet
Copy link
Collaborator

@dsnet dsnet commented Dec 16, 2019

The EquateErrors helper equates errors according to errors.Is.
We also declare a sentinel AnyError value that matches any
non-nil error value.

This adds a dependency on golang.org/x/xerrors so that we can
continue to suppport go1.8, which is our current minimally
supported version of Go.

Fixes #89

@@ -123,3 +124,36 @@ func (a timeApproximator) compare(x, y time.Time) bool {
// Note: time.Time doesn't have AfterOrEqual method hence the negation.
return !x.Add(a.margin).Before(y)
}

// AnyError is an error that matches all other non-nil errors.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AnyError matches itself as well, so perhaps "...that matches any non-nil error."?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

func (anyError) Is(err error) bool { return err != nil }

// EquateErrors returns a Comparer option that determines errors to be equal
// if errors.Is reports them to be equal. The AnyError error can be used to
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"...if errors.Is reports them to match."

I've been trying (not always successfully) to use the verb "match" for the test performed by errors.Is.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

The EquateErrors helper equates errors according to errors.Is.
We also declare a sentinel AnyError value that matches any
non-nil error value.

This adds a dependency on golang.org/x/xerrors so that we can
continue to suppport go1.8, which is our current minimally
supported version of Go.
@dsnet dsnet merged commit 340f1eb into master Dec 16, 2019
@dsnet dsnet deleted the equate-errors branch December 16, 2019 17:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

proposal: cmpopts: add convenience functions for errors
2 participants