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
cmdutil: Support errors.Join-based multi-errors #13813
Conversation
Changelog[uncommitted] (2023-09-01)Miscellaneous
|
sdk/go/common/util/cmdutil/exit.go
Outdated
@@ -160,18 +161,38 @@ func exitErrorCodef(code int, format string, args ...interface{}) { | |||
os.Exit(code) | |||
} | |||
|
|||
type stdMultiError interface { |
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.
Is this really the "correct" way to detect an errors.Join 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.
Yeah. They didn't export an interface or a function to break apart a joined error.
They only documented the contract that Unwrap() []error
= joined error of any kind (including fmt.Errorf
with multiple %w
s).
The contract is mostly used by errors.Is
and errors.As
to iterate into the error tree.
https://cs.opensource.google/go/go/+/refs/tags/go1.21.0:src/errors/wrap.go;l=57-63
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.
switch x := err.(type) {
case interface{ Unwrap() []error }:
That's maybe a neater way to do it here as well rather than adding a new name to the package.
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 prefer the inline interface too. I may have gone with the named one for documentation purposes. A comment will suffice for the inline one, though. I'll switch to it.
cmdutil has some special handling for hashicorp/go-multierror so that multi-errors are printed cleanly in the form: %d errors occurred: 1) foo 2) bar ... In Go 1.20, the errors package got a native `errors.Join` function. This adds support for errors.Join-based multi-errors to this logic. These errors implement an `Unwrap() []error` method which can be used to access the full list of errors. We use that and then implement the same logic for formatting as before.
fea8fc5
to
b7fd2ec
Compare
cmdutil has some special handling for hashicorp/go-multierror
so that multi-errors are printed cleanly in the form:
In Go 1.20, the errors package got a native
errors.Join
function.This adds support for errors.Join-based multi-errors to this logic.
These errors implement an
Unwrap() []error
methodwhich can be used to access the full list of errors.
We use that and then implement the same logic for formatting as before.