Skip to content

Commit 110c79f

Browse files
authored
chore(cli): improve error message for cdk migrate (#29392)
### Reason for this change This change is a follow-up to a [PR](cdklabs/cdk-from-cfn#594) that improved the error message thrown by `cdk-from-cfn` when an invalid resource property was used in a CloudFormation template. This PR further improves the error message on the cli side. ### Description of changes Primarily, this PR is a verbiage change. The base error message now states that the `<stack-name> could not be generated because <error-message>`. The error message itself is checked against `unreachable` because any use of `panic!`, `unreachable!`, or `unimplemented!` will cause the `cdk-from-cfn` source code to panic in-place. In the resulting wasm binary, this produces a `RuntimeError` that has an error message of `unreachable`. I've improved this slightly by stating `template and/or language inputs caused the source code to panic`. If the error message is not `unreachable`, then the error message is taken as is with `TransmuteError:` replaced. Note that we should still continue to improve our error messages in `cdk-from-cfn` by by replacing `panic!`, `unreachable!`, and `unimplemented!` with more detailed error messages. ### Description of how you validated changes An existing unit test was changed based on the error message verbiage change. Additionally, a new unit test was added to validate that the expected error message would be thrown by the cli when an invalid resource property was used in a CloudFormation template. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 1f8acc1 commit 110c79f

File tree

8 files changed

+39
-12
lines changed

8 files changed

+39
-12
lines changed

Diff for: packages/@aws-cdk/cli-lib-alpha/THIRD_PARTY_LICENSES

+1-1
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
668668

669669
----------------
670670

671-
** cdk-from-cfn@0.133.0 - https://www.npmjs.com/package/cdk-from-cfn/v/0.133.0 | MIT OR Apache-2.0
671+
** cdk-from-cfn@0.141.0 - https://www.npmjs.com/package/cdk-from-cfn/v/0.141.0 | MIT OR Apache-2.0
672672

673673
----------------
674674

Diff for: packages/aws-cdk/THIRD_PARTY_LICENSES

+1-1
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
461461

462462
----------------
463463

464-
** cdk-from-cfn@0.133.0 - https://www.npmjs.com/package/cdk-from-cfn/v/0.133.0 | MIT OR Apache-2.0
464+
** cdk-from-cfn@0.141.0 - https://www.npmjs.com/package/cdk-from-cfn/v/0.141.0 | MIT OR Apache-2.0
465465

466466
----------------
467467

Diff for: packages/aws-cdk/lib/commands/migrate.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,14 @@ export async function generateCdkApp(stackName: string, stack: string, language:
8282
* @returns A string representation of a CDK stack file
8383
*/
8484
export function generateStack(template: string, stackName: string, language: string) {
85+
const formattedStackName = `${camelCase(decamelize(stackName), { pascalCase: true })}Stack`;
8586
try {
86-
const formattedStackName = `${camelCase(decamelize(stackName), { pascalCase: true })}Stack`;
8787
return cdk_from_cfn.transmute(template, language, formattedStackName);
8888
} catch (e) {
89-
throw new Error(`stack generation failed due to error '${(e as Error).message}'`);
89+
const errorMessage = (e as Error).message === 'unreachable'
90+
? 'template and/or language inputs caused the source code to panic'
91+
: (e as Error).message.replace('TransmuteError: ', '');
92+
throw new Error(`${formattedStackName} could not be generated because ${errorMessage}`);
9093
}
9194
}
9295

Diff for: packages/aws-cdk/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
"aws-sdk": "^2.1562.0",
106106
"camelcase": "^6.3.0",
107107
"cdk-assets": "0.0.0",
108-
"cdk-from-cfn": "^0.133.0",
108+
"cdk-from-cfn": "^0.141.0",
109109
"chalk": "^4",
110110
"chokidar": "^3.6.0",
111111
"decamelize": "^5.0.1",

Diff for: packages/aws-cdk/test/cdk-toolkit.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -918,8 +918,8 @@ describe('synth', () => {
918918
stackName: 'cannot-generate-template',
919919
fromPath: path.join(__dirname, 'commands', 'test-resources', 'templates', 'sqs-template.json'),
920920
language: 'rust',
921-
})).rejects.toThrowError('stack generation failed due to error \'unreachable\'');
922-
expect(stderrMock.mock.calls[1][0]).toContain(' ❌ Migrate failed for `cannot-generate-template`: stack generation failed due to error \'unreachable\'');
921+
})).rejects.toThrowError('CannotGenerateTemplateStack could not be generated because template and/or language inputs caused the source code to panic');
922+
expect(stderrMock.mock.calls[1][0]).toContain(' ❌ Migrate failed for `cannot-generate-template`: CannotGenerateTemplateStack could not be generated because template and/or language inputs caused the source code to panic');
923923
});
924924

925925
cliTest('migrate succeeds for valid template from local path when no lanugage is provided', async (workDir) => {

Diff for: packages/aws-cdk/test/commands/migrate.test.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ describe('Migrate Function Tests', () => {
2121

2222
const validTemplatePath = path.join(...templatePath, 's3-template.json');
2323
const emptyTemplatePath = path.join(...templatePath, 'empty-template.yml');
24+
const invalidTemplatePath = path.join(...templatePath, 'rds-template.json');
2425
const validTemplate = readFromPath(validTemplatePath)!;
26+
const invalidTemplate = readFromPath(invalidTemplatePath)!;
2527

2628
beforeEach(async () => {
2729
sdkProvider = new MockSdkProvider();
@@ -132,7 +134,11 @@ describe('Migrate Function Tests', () => {
132134
});
133135

134136
test('generateStack throws error when called for other language', () => {
135-
expect(() => generateStack(validTemplate, 'BadBadBad', 'php')).toThrowError('stack generation failed due to error \'unreachable\'');
137+
expect(() => generateStack(validTemplate, 'BadBadBad', 'php')).toThrowError('BadBadBadStack could not be generated because template and/or language inputs caused the source code to panic');
138+
});
139+
140+
test('generateStack throws error for invalid resource property', () => {
141+
expect(() => generateStack(invalidTemplate, 'VeryBad', 'typescript')).toThrow('VeryBadStack could not be generated because ReadEndpoint is not a valid property for resource RDSCluster of type AWS::RDS::DBCluster');
136142
});
137143

138144
cliTest('generateCdkApp generates the expected cdk app when called for typescript', async (workDir) => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"AWSTemplateFormatVersion": "2010-09-09",
3+
"Resources": {
4+
"RDSCluster": {
5+
"Type": "AWS::RDS::DBCluster",
6+
"Properties": {
7+
"DBClusterIdentifier" : "aurora-postgresql-cluster",
8+
"Engine" : "aurora-postgresql",
9+
"EngineVersion" : "10.7",
10+
"DBClusterParameterGroupName" : "default.aurora-postgresql10",
11+
"EnableCloudwatchLogsExports" : ["postgresql"],
12+
"ReadEndpoint": {
13+
"Address": "http://127.0.0.1:8080"
14+
}
15+
}
16+
}
17+
}
18+
}

Diff for: yarn.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -6307,10 +6307,10 @@ [email protected], case@^1.6.3:
63076307
resolved "https://registry.npmjs.org/case/-/case-1.6.3.tgz#0a4386e3e9825351ca2e6216c60467ff5f1ea1c9"
63086308
integrity sha512-mzDSXIPaFwVDvZAHqZ9VlbyF4yyXRuX6IvB06WvPYkqJVO24kX1PPhv9bfpKNFZyxYFmmgo03HUiD8iklmJYRQ==
63096309

6310-
cdk-from-cfn@^0.133.0:
6311-
version "0.133.0"
6312-
resolved "https://registry.npmjs.org/cdk-from-cfn/-/cdk-from-cfn-0.133.0.tgz#a18dd2c505c8fc0b7f4947f6d6afb3e7fea5b391"
6313-
integrity sha512-Yj0kE+GixlSGLNTodCaZEcIeyxzNQonZAykmsSP7wRSm3yYNfg/2XX1tRWi9hzUGMwqYuQr5GsCBymukU0GKsA==
6310+
cdk-from-cfn@^0.141.0:
6311+
version "0.141.0"
6312+
resolved "https://registry.npmjs.org/cdk-from-cfn/-/cdk-from-cfn-0.141.0.tgz#f480ee87658f8056a933e4684198b050cb783e8f"
6313+
integrity sha512-saEHn8rxR3a2b0MpOAjnNMeLOoCKo9lSKYMm7GsAPlzyMOiTmTQeIwKhUAgt8ouVbt4VZPddBukzmfh5+xtCYw==
63146314

63156315
cdk-generate-synthetic-examples@^0.1.304:
63166316
version "0.1.304"

0 commit comments

Comments
 (0)