From 05756d4b2eceaca8a35d183774c9fc03dd0643ca Mon Sep 17 00:00:00 2001 From: Pinglei Guo <72180622+pingleig@users.noreply.github.com> Date: Mon, 24 Jan 2022 08:52:00 -0800 Subject: [PATCH] config: Return error from optFns in LoadDefaultConfig (#1562) Adds handling of return error in LoadDefaultConfig when processing functional options for LoadOptions. --- .changelog/450d4ca924cb43e1aaa66310a96b90d1.json | 8 ++++++++ config/config.go | 4 +++- config/config_test.go | 11 +++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 .changelog/450d4ca924cb43e1aaa66310a96b90d1.json diff --git a/.changelog/450d4ca924cb43e1aaa66310a96b90d1.json b/.changelog/450d4ca924cb43e1aaa66310a96b90d1.json new file mode 100644 index 00000000000..b57e5cf33ea --- /dev/null +++ b/.changelog/450d4ca924cb43e1aaa66310a96b90d1.json @@ -0,0 +1,8 @@ +{ + "id": "450d4ca9-24cb-43e1-aaa6-6310a96b90d1", + "type": "bugfix", + "description": "Fixes LoadDefaultConfig handling of errors returned by passed in functional options. Previously errors returned from the LoadOptions passed into LoadDefaultConfig were incorrectly ignored. [#1562](https://github.com/aws/aws-sdk-go-v2/pull/1562). Thanks to [Pinglei Guo](https://github.com/pingleig) for submitting this PR.", + "modules": [ + "config" + ] +} \ No newline at end of file diff --git a/config/config.go b/config/config.go index 80d6e83a4d3..44b88db608a 100644 --- a/config/config.go +++ b/config/config.go @@ -181,7 +181,9 @@ func (cs configs) ResolveConfig(f func(configs []interface{}) error) error { func LoadDefaultConfig(ctx context.Context, optFns ...func(*LoadOptions) error) (cfg aws.Config, err error) { var options LoadOptions for _, optFn := range optFns { - optFn(&options) + if err := optFn(&options); err != nil { + return aws.Config{}, err + } } // assign Load Options to configs diff --git a/config/config_test.go b/config/config_test.go index ea65b58c999..0638fb96779 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -2,6 +2,7 @@ package config import ( "context" + "fmt" "testing" "github.com/aws/aws-sdk-go-v2/aws" @@ -135,3 +136,13 @@ func TestConfigs_ResolveAWSConfig(t *testing.T) { t.Errorf("expect config sources match, got diff: \n %s", diff) } } + +func TestLoadDefaultConfig(t *testing.T) { + optWithErr := func(_ *LoadOptions) error { + return fmt.Errorf("some error") + } + _, err := LoadDefaultConfig(context.TODO(), optWithErr) + if err == nil { + t.Fatal("expect error when optFn returns error, got nil") + } +}