Skip to content

Support multierr.Every#78

Merged
JacobOaks merged 3 commits intouber-go:masterfrom
JacobOaks:every
Mar 28, 2023
Merged

Support multierr.Every#78
JacobOaks merged 3 commits intouber-go:masterfrom
JacobOaks:every

Conversation

@JacobOaks
Copy link
Copy Markdown
Contributor

@JacobOaks JacobOaks commented Mar 28, 2023

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.

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.

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.
Comment thread error_test.go
wantEvery: false,
},
{
desc: "multiErr w multiErr target",
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice.

Comment thread error_test.go
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 28, 2023

Codecov Report

Merging #78 (7b893ed) into master (d42b7a1) will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##            master       #78   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            3         3           
  Lines          117       120    +3     
=========================================
+ Hits           117       120    +3     
Impacted Files Coverage Δ
error.go 100.00% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@JacobOaks JacobOaks merged commit d8067ab into uber-go:master Mar 28, 2023
@JacobOaks JacobOaks deleted the every branch March 28, 2023 20:51
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

Successfully merging this pull request may close these issues.

3 participants