Skip to content
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

Merged
merged 1 commit into from Sep 1, 2023

Conversation

abhinav
Copy link
Contributor

@abhinav abhinav commented Aug 29, 2023

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.

@pulumi-bot
Copy link
Contributor

pulumi-bot commented Aug 29, 2023

Changelog

[uncommitted] (2023-09-01)

Miscellaneous

  • [sdk/go] Support multi-errors built from errors.Join for RunFunc, Exit, and friends.
    #13813

@abhinav abhinav requested a review from a team August 29, 2023 16:51
@abhinav abhinav enabled auto-merge August 30, 2023 15:47
@abhinav abhinav disabled auto-merge August 30, 2023 15:47
@@ -160,18 +161,38 @@ func exitErrorCodef(code int, format string, args ...interface{}) {
os.Exit(code)
}

type stdMultiError interface {
Copy link
Member

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?

Copy link
Contributor Author

@abhinav abhinav Sep 1, 2023

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 %ws).

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

Copy link
Member

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.

Copy link
Contributor Author

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.
@abhinav abhinav added this pull request to the merge queue Sep 1, 2023
Merged via the queue into master with commit 615bd0f Sep 1, 2023
45 checks passed
@abhinav abhinav deleted the abhinav/cmdutil-err-join branch September 1, 2023 19:42
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.

None yet

3 participants