-
Notifications
You must be signed in to change notification settings - Fork 2.1k
use native errors.Join for multi-errors on "docker ps", "docker node ls" #6440
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
Conversation
Signed-off-by: Sebastiaan van Stijn <[email protected]>
Signed-off-by: Sebastiaan van Stijn <[email protected]>
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
| var ( | ||
| warns []string | ||
| errs []string | ||
| errs []error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is possible to use errors.Join directly on the error type instead of a slice.
var errs error
errs = errors.Join(errs, err)
errs = errors.Join(errs, err)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, they're subtly different though; repeatedly calling errors.Join effectively is similar to errors.Wrap; the new error is nesting previous errors, whereas a slice approach makes them all "peers"; https://go.dev/play/p/42cXzTSkjoA
package main
import (
"errors"
"fmt"
)
func main() {
errs := []error{
errors.New("one"),
errors.New("two"),
errors.New("three"),
}
var nested error
for _, err := range errs {
nested = errors.Join(nested, err)
}
flat := errors.Join(errs...)
fmt.Printf("Flat : %#v\n", flat)
fmt.Printf("Nested: %#v\n", nested)
}Output;
Flat : &errors.joinError{errs:[]error{(*errors.errorString)(0xc000012040), (*errors.errorString)(0xc000012050), (*errors.errorString)(0xc000012060)}}
Nested: &errors.joinError{errs:[]error{(*errors.joinError)(0xc00000e060), (*errors.errorString)(0xc000012060)}}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
interesting, i thought it might've behaved the same
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think they chose a bad name for the function; I was confused as well when I was pointed at it! Join feels like "same level", not "wrap errors to be nested".
In many cases it doesn't make a big difference, e.g. errors.Is will recurse, so will still find nested errors (which sometimes is a bit of a pain / unwanted as well), but sometimes it may matter ("peer" errors vs "nested errors")
- What I did
- How I did it
- How to verify it
- Human readable description for the release notes
- A picture of a cute animal (not mandatory but encouraged)