Comments (4)
Hi @gnahckire, it's intentional that this package does not handle errors by default since there's no sensible policy that works for every use case, and it's the responsibility of the user to define the one that makes sense.
Some simple comparers that you could use:
// equateNilError reports errors to be equal only if both are nil.
equateNilError := cmp.Comparer(func(x, y error) bool {
return x == nil && y == nil
})
// equateAnyError reports errors to be equal only if both are nil or both are non-nil.
equateAnyError := cmp.Comparer(func(x, y error) bool {
return (x == nil) == (y == nil)
})
What @shurcooL suggests is a possibility, but I want to warn you that the error message provided by most packages is not stable, and something as simple as a grammar change can break your test. If you are simply checking that error messages from your own package match, then this approach is fine.
// equateErrorMessage reports errors to be equal if both are nil
// or both have the same message.
equateErrorMessage := cmp.Comparer(func(x, y error) bool {
if x == nil || y == nil {
return x == nil && y == nil
}
return x.Error() == y.Error()
})
Even more advanced, if you know properties about the errors you expect, you can write better equate functions:
// sentinelErrors is a set of common sentinel errors
// (i.e., those that are safe to compare directly using the == operator.
sentinelErrors := map[error]bool{io.EOF: true, io.ErrUnexpectedEOF: true, ...}
equateError := cmp.Comparer(func(x, y error) bool {
if x == nil || y == nil {
return x == nil && y == nil
}
// Check if x and y are sentinel errors.
if sentinelErrors[x] || sentinelErrors[y] {
return x == y
}
// Compare error based on their properties.
switch {
case os.IsExist(x) || os.IsExist(y):
return os.IsExist(x) && os.IsExist(y)
case os.IsPathSeparator(x) || os.IsPathSeparator(y):
return os.IsPathSeparator(x) && os.IsPathSeparator(y)
case os.IsPermission(x) || os.IsPermission(y):
return os.IsPermission(x) && os.IsPermission(y)
}
// Default to saying that errors are equal
return true
})
As you can imagine, the set of possible errors in Go is infinite, so it does not make sense for the cmp
package to provide the functionality to handle them. It is the users responsibility to know how to properly compare the errors they are interested in.
from go-cmp.
Since it may be relevant, here's something I've used when I wanted to compare error (strings):
// equalError reports whether errors a and b are considered equal.
// They're equal if both are nil, or both are not nil and a.Error() == b.Error().
func equalError(a, b error) bool {
return a == nil && b == nil || a != nil && b != nil && a.Error() == b.Error()
}
from go-cmp.
Thanks! I'm doing that currently. Just double checking to make sure I wasn't missing something.
from go-cmp.
Awesome. Thanks!
from go-cmp.
Related Issues (20)
- New minor release of the library? HOT 4
- cmp.Diff() switching from property diffs to "new object" diff when object changes significantly HOT 1
- Inconsistent whitespace produced by cmp.Diff HOT 2
- cmpopts: add EquateUnsetFields(want) or FilterFieldsFrom(want) HOT 1
- IgnoreFields and nested struct HOT 2
- Enable a dependency update tool HOT 2
- Package Name Mismatches HOT 4
- unhelpful output for difference in slices HOT 1
- How to implement jests.js's `expect.any(String)` like functionality? HOT 1
- cmpopts.IgnoreFields does not work on structs with an Equal method HOT 1
- Performance Issue Comparing Array Types HOT 1
- Naming clash for new cmp standard package HOT 4
- Comparing two fields with diffirent types that are semantically equivalent HOT 3
- String diff for UUIDs can use strings.Join syntax which makes it more confusing HOT 4
- Reporter truncating important context on diff output but leaving others alone
- How to add more context to Diff output?
- Error at init when running on Windows ARM HOT 3
- `cmp.Diff`: want to compare unexport structs like `reflect.DeepEqual`
- Comparer doesn't run when type is wrapped in empty interface HOT 2
- Pointers as Map index keys are not respected as equal HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-cmp.