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

chore(cli): improve error message for cdk migrate #29392

Merged
merged 9 commits into from
Mar 8, 2024
Merged
7 changes: 5 additions & 2 deletions packages/aws-cdk/lib/commands/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,14 @@ export async function generateCdkApp(stackName: string, stack: string, language:
* @returns A string representation of a CDK stack file
*/
export function generateStack(template: string, stackName: string, language: string) {
const formattedStackName = `${camelCase(decamelize(stackName), { pascalCase: true })}Stack`;
try {
const formattedStackName = `${camelCase(decamelize(stackName), { pascalCase: true })}Stack`;
return cdk_from_cfn.transmute(template, language, formattedStackName);
} catch (e) {
throw new Error(`stack generation failed due to error '${(e as Error).message}'`);
const errorMessage = (e as Error).message === 'unreachable'
? 'template and/or language inputs caused the source code to panic'
: (e as Error).message.replace('TransmuteError: ', '');
throw new Error(`${formattedStackName} could not be generated because ${errorMessage}`);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
"aws-sdk": "^2.1562.0",
"camelcase": "^6.3.0",
"cdk-assets": "0.0.0",
"cdk-from-cfn": "^0.133.0",
"cdk-from-cfn": "^0.140.0",
"chalk": "^4",
"chokidar": "^3.6.0",
"decamelize": "^5.0.1",
Expand Down
8 changes: 7 additions & 1 deletion packages/aws-cdk/test/commands/migrate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ describe('Migrate Function Tests', () => {

const validTemplatePath = path.join(...templatePath, 's3-template.json');
const emptyTemplatePath = path.join(...templatePath, 'empty-template.yml');
const invalidTemplatePath = path.join(...templatePath, 'rds-template.json');
const validTemplate = readFromPath(validTemplatePath)!;
const invalidTemplate = readFromPath(invalidTemplatePath)!;

beforeEach(async () => {
sdkProvider = new MockSdkProvider();
Expand Down Expand Up @@ -132,7 +134,11 @@ describe('Migrate Function Tests', () => {
});

test('generateStack throws error when called for other language', () => {
expect(() => generateStack(validTemplate, 'BadBadBad', 'php')).toThrowError('stack generation failed due to error \'unreachable\'');
expect(() => generateStack(validTemplate, 'BadBadBad', 'php')).toThrowError('BadBadBadStack could not be generated because template and/or language inputs caused the source code to panic');
});

test('generateStack throws error for invalid resource property', () => {
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');
});

cliTest('generateCdkApp generates the expected cdk app when called for typescript', async (workDir) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"AWSTemplateFormatVersion": "2010-09-09",
"Resources": {
"RDSCluster": {
"Type": "AWS::RDS::DBCluster",
"Properties": {
"DBClusterIdentifier" : "aurora-postgresql-cluster",
"Engine" : "aurora-postgresql",
"EngineVersion" : "10.7",
"DBClusterParameterGroupName" : "default.aurora-postgresql10",
"EnableCloudwatchLogsExports" : ["postgresql"],
"ReadEndpoint": {
"Address": "http://127.0.0.1:8080"
}
}
}
}
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6307,10 +6307,10 @@ case@1.6.3, case@^1.6.3:
resolved "https://registry.npmjs.org/case/-/case-1.6.3.tgz#0a4386e3e9825351ca2e6216c60467ff5f1ea1c9"
integrity sha512-mzDSXIPaFwVDvZAHqZ9VlbyF4yyXRuX6IvB06WvPYkqJVO24kX1PPhv9bfpKNFZyxYFmmgo03HUiD8iklmJYRQ==

cdk-from-cfn@^0.133.0:
version "0.133.0"
resolved "https://registry.npmjs.org/cdk-from-cfn/-/cdk-from-cfn-0.133.0.tgz#a18dd2c505c8fc0b7f4947f6d6afb3e7fea5b391"
integrity sha512-Yj0kE+GixlSGLNTodCaZEcIeyxzNQonZAykmsSP7wRSm3yYNfg/2XX1tRWi9hzUGMwqYuQr5GsCBymukU0GKsA==
cdk-from-cfn@^0.140.0:
version "0.140.0"
resolved "https://registry.npmjs.org/cdk-from-cfn/-/cdk-from-cfn-0.140.0.tgz#c9cbd96f0f559adfbd1874c03f2ac623427ef6b3"
integrity sha512-i9RjfOeEZDyKqI2FLbZgxuV5FwDkipv/3AqiSSw3TtLaIWi3qQ17GPrVOekINLqC4OgT7tCiMWawswwrM/sAaQ==

cdk-generate-synthetic-examples@^0.1.304:
version "0.1.304"
Expand Down
Loading