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

Return errors on parse failures for config.Try* #9407

Merged
merged 13 commits into from Apr 20, 2022

Conversation

iwahbe
Copy link
Member

@iwahbe iwahbe commented Apr 15, 2022

Description

config.Try* returns an error message if a config key is missing, but there is currently no way to guarantee that a key is pulled from the config. When a non-string primitive key is tried, a failure to parse leads returns the type appropriate zero value. This is super counterintuitive to me. Especially since config.TryObject does return a parse error when appropriate.

This is a breaking change, but it is hard to imagine a situation where config.Try* is used correctly. Therefore I believe the change is appropriate.

This PR changes the behavior of config.Try* to return an error if either the requested key is missing in the config or if the key is invalid for the requested type.

Example

cfg := config.New(ctx, "dev")
v, err := cfg.TryInt("key")
if errors.Is(err, config.ErrMissingVar) {
    // Set a default
    v = 42
} else if err != nil {
    // We set the value incorrectly
    panic(fmt.Errorf(`Attempted to pass default value "%s" as float64: %w`, cfg.Require("key"), err))
}

PS: We do the same thing for config.Require*, and I made the same change.

Checklist

  • I have added tests that prove my fix is effective or that my feature works
  • Yes, there are changes in this PR that warrants bumping the Pulumi Service API version

@iwahbe iwahbe self-assigned this Apr 15, 2022
@iwahbe iwahbe requested review from a team, Frassle and Zaid-Ajaj April 15, 2022 12:35
Copy link
Member

@pgavlin pgavlin left a comment

Choose a reason for hiding this comment

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

CHANGELOG_PENDING needs to be cleaned up; LGTM otherwise.

CHANGELOG_PENDING.md Outdated Show resolved Hide resolved
sdk/go/pulumi/config/require.go Outdated Show resolved Hide resolved
@iwahbe iwahbe requested a review from pgavlin April 18, 2022 10:10
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

func failf(format string, a ...interface{}) {
panic(fmt.Sprintf(format, a...))
Copy link
Member

Choose a reason for hiding this comment

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

Nit: I'd panic with an error here to make it a bit easier to recover if necessary.

Suggested change
panic(fmt.Sprintf(format, a...))
panic(fmt.Errorf(format, a...))

@github-actions
Copy link

Diff for pulumi-azuread with merge commit 2ce4e97

@github-actions
Copy link

Diff for pulumi-random with merge commit 2ce4e97

@github-actions
Copy link

Diff for pulumi-kubernetes with merge commit 2ce4e97

@github-actions
Copy link

Diff for pulumi-gcp with merge commit 2ce4e97

@github-actions
Copy link

Diff for pulumi-azure with merge commit 2ce4e97

@github-actions
Copy link

Diff for pulumi-aws with merge commit 2ce4e97

@github-actions
Copy link

Diff for pulumi-random with merge commit a14052d

@github-actions
Copy link

Diff for pulumi-azuread with merge commit a14052d

@github-actions
Copy link

Diff for pulumi-kubernetes with merge commit a14052d

@github-actions
Copy link

Diff for pulumi-gcp with merge commit a14052d

@github-actions
Copy link

Diff for pulumi-azuread with merge commit b3ada87

@github-actions
Copy link

Diff for pulumi-random with merge commit b3ada87

@github-actions
Copy link

Diff for pulumi-kubernetes with merge commit b3ada87

@github-actions
Copy link

Diff for pulumi-azure with merge commit a14052d

@github-actions
Copy link

Diff for pulumi-aws with merge commit a14052d

@github-actions
Copy link

Diff for pulumi-gcp with merge commit b3ada87

@github-actions
Copy link

Diff for pulumi-azure with merge commit b3ada87

sdk/go/pulumi/templates/config-require.go.template Outdated Show resolved Hide resolved
sdk/go/pulumi/templates/config-require.go.template Outdated Show resolved Hide resolved
pkg/codegen/pcl/rewrite_convert.go Outdated Show resolved Hide resolved
@iwahbe iwahbe merged commit 58ca844 into master Apr 20, 2022
@pulumi-bot pulumi-bot deleted the iwahbe/improve-go-try-errors branch April 20, 2022 07:46
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