-
Notifications
You must be signed in to change notification settings - Fork 41
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 multierr.Every #66
Comments
We agree that this would be a good convenience feature to add to multierr, but we're still considering if this should work with |
FWIW, the standard library's Perhaps we need an "all" variant of Is ( |
Internal ref: GO-1932 |
This PR introduces a new exported function in the multierr package called `Every`, which checks every error within a given error against a given target using `errors.Is`, and only returns true if all checks return true. ```go err := multierr.Combine(ErrIgnorable, errors.New("great sadness")) fmt.Println(multierr.Every(err, ErrIgnorable)) // output = "false" err := multierr.Combine(ErrIgnorable, ErrIgnorable) fmt.Println(multierr.Every(err, ErrIgnorable) // output = "true" ``` This also works when the error passed in as the first argument is not a `multiErr`, including when it is a go1.20+ error that implements `Unwrap() []error`. This solves uber-go#66.
This PR introduces a new exported function in the multierr package called `Every`, which checks every error within a given error against a given target using `errors.Is`, and only returns true if all checks return true. ```go err := multierr.Combine(ErrIgnorable, errors.New("great sadness")) fmt.Println(errors.Is(err, ErrIgnorable)) // output = "true" fmt.Println(multierr.Every(err, ErrIgnorable)) // output = "false" err := multierr.Combine(ErrIgnorable, ErrIgnorable) fmt.Println(errors.Is(err, ErrIgnorable)) // output = "true" fmt.Println(multierr.Every(err, ErrIgnorable)) // output = "true" ``` This also works when the error passed in as the first argument is not a `multiErr`, including when it is a go1.20+ error that implements `Unwrap() []error`. This solves #66.
Hello,
There seems to be a correctness issue the implementation of
multiError.Is
. I say "seems" because I cannot find any consensus on whaterrors.Is(err, ErrIgnorable)
should actually be doing whenerr
is amultiError
containing both ErrIgnorable and an important error (that you do want to handle).Perhaps
Is
should only return true if all contained errors match the target, so insteadan alternative would be that
Is
is not implemented at all, and thenerrors.Is
should return false.Again, I don't know if the golang developers ever specified some semantics for
errors.Is
, but I think that from both an intuitive and practical sense,errors.Is(err, ErrIgnorable)
should return false. Intuitive, because a multiError is not equal to a single error, and practical because if the important error is not represented by a value, e.g.val ErrImportant = errors.New(...)
, then there is no way to easily ignore only ignorable error without callingErrors()
and iterating over the individual errors..The text was updated successfully, but these errors were encountered: