Git Product home page Git Product logo

Comments (4)

dsnet avatar dsnet commented on May 3, 2024 3

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.

dmitshur avatar dmitshur commented on May 3, 2024

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.

gnahckire avatar gnahckire commented on May 3, 2024

Thanks! I'm doing that currently. Just double checking to make sure I wasn't missing something.

from go-cmp.

gnahckire avatar gnahckire commented on May 3, 2024

Awesome. Thanks!

from go-cmp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.