go-check / check

Rich testing for the Go language
Other
694 stars 178 forks source link

Support for `errors.Is` Checker implementation #139

Open ggambetti opened 3 months ago

ggambetti commented 3 months ago

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 https://github.com/go-git/go-git/issues/1097 and https://github.com/go-git/go-git/blob/dcf0639a168c441de6753c6644322e6b619499ae/plumbing/transport/http/common.go#L414-L441 for some context.

barrettj12 commented 1 month ago

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 commented 1 month ago

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.