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

Support for errors.Is Checker implementation #139

Open
ggambetti opened this issue May 24, 2024 · 2 comments
Open

Support for errors.Is Checker implementation #139

ggambetti opened this issue May 24, 2024 · 2 comments

Comments

@ggambetti
Copy link

I've run into an issue recently where I've had to move some code from returning a static error to wrapping that error, and noticed that there's not a builtin way to compare errors using the errors.Is function. Would it be possible to add an ErrorIs (or similarly named) Checker implementation that uses errors.Is to assert error similarity?


I can't continue to use ErrorMatches in this case because it doesn't capture the correct meaning of my error check. I want to know that I'm returning a specific kind of error even if it's wrapped or the message is modified.

eg:

// Previously
return package.StaticErrorValue

// Now
return fmt.Errorf("%w: %s", package.StaticErrorValue, reason)

// Tests:
c.Assert(err, ErrorIs, package.StaticErrorValue)

This is coming from go-git/go-git#1097 and https://github.com/go-git/go-git/blob/dcf0639a168c441de6753c6644322e6b619499ae/plumbing/transport/http/common.go#L414-L441 for some context.

@ggambetti ggambetti changed the title Support for errors.Is matcher Support for errors.Is Checker implementation May 24, 2024
@barrettj12
Copy link

You can define your own custom checkers - they just need to satisfy the Checker interface. We've done exactly what you're asking here:
https://github.com/juju/testing/blob/e8c2c6dee228df02bae64e27d1715fe62b682bb9/checkers/error.go

@ggambetti
Copy link
Author

That's what I implemented in the linked PR around the time I filed this issue: https://github.com/go-git/go-git/pull/1100/files#diff-df7610266a25d996bedf7b6be25b5b08e69445cdeb262833ea9be5c3bf1fdc6f.

I'm of the opinion that errors.Is is sufficiently fundamental to error handling in Go that it warrants inclusion into this library.

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

No branches or pull requests

2 participants