From 603b75dd8f988135f2074c0ef3a6f4d42d799248 Mon Sep 17 00:00:00 2001 From: Kamil Sobol Date: Wed, 18 Sep 2024 13:26:07 -0700 Subject: [PATCH] Classify pointing client config generator at metadata-less stack as user error (#2024) --- .changeset/poor-owls-listen.md | 5 +++ .../unified_client_config_generator.test.ts | 33 +++++++++++++++++++ .../src/unified_client_config_generator.ts | 14 ++++++++ 3 files changed, 52 insertions(+) create mode 100644 .changeset/poor-owls-listen.md diff --git a/.changeset/poor-owls-listen.md b/.changeset/poor-owls-listen.md new file mode 100644 index 0000000000..f7ab95c922 --- /dev/null +++ b/.changeset/poor-owls-listen.md @@ -0,0 +1,5 @@ +--- +'@aws-amplify/client-config': patch +--- + +Classify pointing client config generator at metadata-less stack as user error diff --git a/packages/client-config/src/unified_client_config_generator.test.ts b/packages/client-config/src/unified_client_config_generator.test.ts index 9f1734ca2a..4e6f0b0aed 100644 --- a/packages/client-config/src/unified_client_config_generator.test.ts +++ b/packages/client-config/src/unified_client_config_generator.test.ts @@ -449,6 +449,39 @@ void describe('UnifiedClientConfigGenerator', () => { ); }); + void it('throws user error if the stack is missing metadata', async () => { + const outputRetrieval = mock.fn(() => { + throw new BackendOutputClientError( + BackendOutputClientErrorType.METADATA_RETRIEVAL_ERROR, + 'Stack template metadata is not a string' + ); + }); + const modelSchemaAdapter = new ModelIntrospectionSchemaAdapter( + stubClientProvider + ); + + const configContributors = new ClientConfigContributorFactory( + modelSchemaAdapter + ).getContributors('1.1'); + + const clientConfigGenerator = new UnifiedClientConfigGenerator( + outputRetrieval, + configContributors + ); + + await assert.rejects( + () => clientConfigGenerator.generateClientConfig(), + (error: AmplifyUserError) => { + assert.strictEqual( + error.message, + 'Stack was not created with Amplify.' + ); + assert.ok(error.resolution); + return true; + } + ); + }); + void it('throws user error if credentials are expired when getting backend outputs', async () => { const outputRetrieval = mock.fn(() => { throw new BackendOutputClientError( diff --git a/packages/client-config/src/unified_client_config_generator.ts b/packages/client-config/src/unified_client_config_generator.ts index eb8397b01b..284bde0097 100644 --- a/packages/client-config/src/unified_client_config_generator.ts +++ b/packages/client-config/src/unified_client_config_generator.ts @@ -66,6 +66,20 @@ export class UnifiedClientConfigGenerator implements ClientConfigGenerator { error ); } + if ( + error instanceof BackendOutputClientError && + error.code === BackendOutputClientErrorType.METADATA_RETRIEVAL_ERROR + ) { + throw new AmplifyUserError( + 'NonAmplifyStackError', + { + message: 'Stack was not created with Amplify.', + resolution: + 'Ensure the CloudFormation stack ID references a main stack created with Amplify, then re-run this command.', + }, + error + ); + } if ( error instanceof BackendOutputClientError && error.code === BackendOutputClientErrorType.CREDENTIALS_ERROR