Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/@aws-cdk/toolkit/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
const baseConfig = require('@aws-cdk/cdk-build-tools/config/eslintrc');
baseConfig.parserOptions.project = __dirname + '/tsconfig.json';

// custom rules
baseConfig.rules['@cdklabs/no-throw-default-error'] = ['error'];
baseConfig.overrides.push({
files: ["./test/**"],
rules: {
"@cdklabs/no-throw-default-error": "off",
},
});

module.exports = baseConfig;
4 changes: 2 additions & 2 deletions packages/@aws-cdk/toolkit/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module.exports = {
coverageThreshold: {
global: {
// this is very sad but we will get better
branches: 27,
statements: 53,
branches: 42,
statements: 69,
},
},
};
6 changes: 6 additions & 0 deletions packages/@aws-cdk/toolkit/lib/toolkit/toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ export class Toolkit extends CloudAssemblySourceBuilder implements AsyncDisposab
const assembly = await this.assemblyFromSource(cx);
ioHost;
assembly;
// temporary
// eslint-disable-next-line @cdklabs/no-throw-default-error
throw new Error('Not implemented yet');
}

Expand All @@ -189,6 +191,8 @@ export class Toolkit extends CloudAssemblySourceBuilder implements AsyncDisposab
const assembly = await this.assemblyFromSource(cx);
const stacks = await assembly.selectStacksV2(options.stacks);
await this.validateStacksMetadata(stacks, ioHost);
// temporary
// eslint-disable-next-line @cdklabs/no-throw-default-error
throw new Error('Not implemented yet');
}

Expand Down Expand Up @@ -619,6 +623,8 @@ export class Toolkit extends CloudAssemblySourceBuilder implements AsyncDisposab
time: synthTime.asMs,
}));

// temporary
// eslint-disable-next-line @cdklabs/no-throw-default-error
throw new Error('Not implemented yet');
}

Expand Down
6 changes: 3 additions & 3 deletions packages/@aws-cdk/toolkit/test/_helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fs from 'node:fs';
import * as path from 'node:path';
import { Toolkit } from '../../lib';
import { Toolkit, ToolkitError } from '../../lib';
import { determineOutputDirectory } from '../../lib/api/cloud-assembly/private';

export * from './test-io-host';
Expand All @@ -12,7 +12,7 @@ function fixturePath(...parts: string[]): string {
export async function appFixture(toolkit: Toolkit, name: string, context?: { [key: string]: any }) {
const appPath = fixturePath(name, 'app.js');
if (!fs.existsSync(appPath)) {
throw new Error(`App Fixture ${name} does not exist in ${appPath}`);
throw new ToolkitError(`App Fixture ${name} does not exist in ${appPath}`);
}
const app = `cat ${appPath} | node --input-type=module`;
return toolkit.fromCdkApp(app, {
Expand All @@ -33,7 +33,7 @@ export function builderFixture(toolkit: Toolkit, name: string, context?: { [key:
export function cdkOutFixture(toolkit: Toolkit, name: string) {
const outdir = path.join(__dirname, '..', '_fixtures', name, 'cdk.out');
if (!fs.existsSync(outdir)) {
throw new Error(`Assembly Dir Fixture ${name} does not exist in ${outdir}`);
throw new ToolkitError(`Assembly Dir Fixture ${name} does not exist in ${outdir}`);
}
return toolkit.fromAssemblyDirectory(outdir);
}
18 changes: 14 additions & 4 deletions packages/aws-cdk/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
const baseConfig = require('@aws-cdk/cdk-build-tools/config/eslintrc');
baseConfig.ignorePatterns.push('lib/init-templates/**/typescript/**/*.ts');
baseConfig.ignorePatterns.push('test/integ/cli/sam_cdk_integ_app/**/*.ts');
baseConfig.parserOptions.project = __dirname + '/tsconfig.json';
const baseConfig = require("@aws-cdk/cdk-build-tools/config/eslintrc");
baseConfig.ignorePatterns.push("lib/init-templates/**/typescript/**/*.ts");
baseConfig.ignorePatterns.push("test/integ/cli/sam_cdk_integ_app/**/*.ts");
baseConfig.parserOptions.project = __dirname + "/tsconfig.json";

// custom rules
baseConfig.rules["@cdklabs/no-throw-default-error"] = ["error"];
baseConfig.overrides.push({
files: ["./test/**"],
rules: {
"@cdklabs/no-throw-default-error": "off",
},
});

module.exports = baseConfig;
15 changes: 8 additions & 7 deletions packages/aws-cdk/lib/api/deploy-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import { determineAllowCrossAccountAssetPublishing } from './util/checks';
import { publishAssets } from '../util/asset-publishing';
import { StringWithoutPlaceholders } from './util/placeholders';
import { ToolkitError } from '../toolkit/error';
import { formatErrorMessage } from '../util/error';

export type DeployStackResult =
Expand Down Expand Up @@ -63,7 +64,7 @@

export function assertIsSuccessfulDeployStackResult(x: DeployStackResult): asserts x is SuccessfulDeployStackResult {
if (x.type !== 'did-deploy-stack') {
throw new Error(`Unexpected deployStack result. This should not happen: ${JSON.stringify(x)}. If you are seeing this error, please report it at https://github.com/aws/aws-cdk/issues/new/choose.`);
throw new ToolkitError(`Unexpected deployStack result. This should not happen: ${JSON.stringify(x)}. If you are seeing this error, please report it at https://github.com/aws/aws-cdk/issues/new/choose.`);
}
}

Expand Down Expand Up @@ -297,7 +298,7 @@
await cfn.deleteStack({ StackName: deployName });
const deletedStack = await waitForStackDelete(cfn, deployName);
if (deletedStack && deletedStack.stackStatus.name !== 'DELETE_COMPLETE') {
throw new Error(
throw new ToolkitError(

Check warning on line 301 in packages/aws-cdk/lib/api/deploy-stack.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/api/deploy-stack.ts#L301

Added line #L301 was not covered by tests
`Failed deleting stack ${deployName} that had previously failed creation (current state: ${deletedStack.stackStatus})`,
);
}
Expand Down Expand Up @@ -455,7 +456,7 @@
};

if (deploymentMethod.method === 'direct' && this.options.resourcesToImport) {
throw new Error('Importing resources requires a changeset deployment');
throw new ToolkitError('Importing resources requires a changeset deployment');

Check warning on line 459 in packages/aws-cdk/lib/api/deploy-stack.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/api/deploy-stack.ts#L459

Added line #L459 was not covered by tests
}

switch (deploymentMethod.method) {
Expand Down Expand Up @@ -669,11 +670,11 @@

// This shouldn't really happen, but catch it anyway. You never know.
if (!successStack) {
throw new Error('Stack deploy failed (the stack disappeared while we were deploying it)');
throw new ToolkitError('Stack deploy failed (the stack disappeared while we were deploying it)');

Check warning on line 673 in packages/aws-cdk/lib/api/deploy-stack.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/api/deploy-stack.ts#L673

Added line #L673 was not covered by tests
}
finalState = successStack;
} catch (e: any) {
throw new Error(suffixWithErrors(formatErrorMessage(e), monitor?.errors));
throw new ToolkitError(suffixWithErrors(formatErrorMessage(e), monitor?.errors));
} finally {
await monitor?.stop();
}
Expand Down Expand Up @@ -748,10 +749,10 @@
await cfn.deleteStack({ StackName: deployName, RoleARN: options.roleArn });
const destroyedStack = await waitForStackDelete(cfn, deployName);
if (destroyedStack && destroyedStack.stackStatus.name !== 'DELETE_COMPLETE') {
throw new Error(`Failed to destroy ${deployName}: ${destroyedStack.stackStatus}`);
throw new ToolkitError(`Failed to destroy ${deployName}: ${destroyedStack.stackStatus}`);

Check warning on line 752 in packages/aws-cdk/lib/api/deploy-stack.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/api/deploy-stack.ts#L752

Added line #L752 was not covered by tests
}
} catch (e: any) {
throw new Error(suffixWithErrors(formatErrorMessage(e), monitor?.errors));
throw new ToolkitError(suffixWithErrors(formatErrorMessage(e), monitor?.errors));

Check warning on line 755 in packages/aws-cdk/lib/api/deploy-stack.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/api/deploy-stack.ts#L755

Added line #L755 was not covered by tests
} finally {
if (monitor) {
await monitor.stop();
Expand Down
19 changes: 10 additions & 9 deletions packages/aws-cdk/lib/api/deployments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
Template,
uploadStackTemplateAssets,
} from './util/cloudformation';
import { ToolkitError } from '../toolkit/error';
import { AssetManifestBuilder } from '../util/asset-manifest-builder';
import {
buildAssets,
Expand Down Expand Up @@ -439,7 +440,7 @@
let deploymentMethod = options.deploymentMethod;
if (options.changeSetName || options.execute !== undefined) {
if (deploymentMethod) {
throw new Error(
throw new ToolkitError(

Check warning on line 443 in packages/aws-cdk/lib/api/deployments.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/api/deployments.ts#L443

Added line #L443 was not covered by tests
"You cannot supply both 'deploymentMethod' and 'changeSetName/execute'. Supply one or the other.",
);
}
Expand Down Expand Up @@ -492,7 +493,7 @@
public async rollbackStack(options: RollbackStackOptions): Promise<RollbackStackResult> {
let resourcesToSkip: string[] = options.orphanLogicalIds ?? [];
if (options.force && resourcesToSkip.length > 0) {
throw new Error('Cannot combine --force with --orphan');
throw new ToolkitError('Cannot combine --force with --orphan');

Check warning on line 496 in packages/aws-cdk/lib/api/deployments.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/api/deployments.ts#L496

Added line #L496 was not covered by tests
}

const env = await this.envs.accessStackForMutableStackOperations(options.stack);
Expand Down Expand Up @@ -564,7 +565,7 @@
return { notInRollbackableState: true };

default:
throw new Error(`Unexpected rollback choice: ${cloudFormationStack.stackStatus.rollbackChoice}`);
throw new ToolkitError(`Unexpected rollback choice: ${cloudFormationStack.stackStatus.rollbackChoice}`);

Check warning on line 568 in packages/aws-cdk/lib/api/deployments.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/api/deployments.ts#L568

Added line #L568 was not covered by tests
}

const monitor = options.quiet
Expand All @@ -580,7 +581,7 @@

// This shouldn't really happen, but catch it anyway. You never know.
if (!successStack) {
throw new Error('Stack deploy failed (the stack disappeared while we were rolling it back)');
throw new ToolkitError('Stack deploy failed (the stack disappeared while we were rolling it back)');

Check warning on line 584 in packages/aws-cdk/lib/api/deployments.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/api/deployments.ts#L584

Added line #L584 was not covered by tests
}
finalStackState = successStack;

Expand All @@ -604,11 +605,11 @@
continue;
}

throw new Error(
throw new ToolkitError(

Check warning on line 608 in packages/aws-cdk/lib/api/deployments.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/api/deployments.ts#L608

Added line #L608 was not covered by tests
`${stackErrorMessage} (fix problem and retry, or orphan these resources using --orphan or --force)`,
);
}
throw new Error(
throw new ToolkitError(

Check warning on line 612 in packages/aws-cdk/lib/api/deployments.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/api/deployments.ts#L612

Added line #L612 was not covered by tests
"Rollback did not finish after a large number of iterations; stopping because it looks like we're not making progress anymore. You can retry if rollback was progressing as expected.",
);
}
Expand Down Expand Up @@ -698,7 +699,7 @@
const publisher = this.cachedPublisher(assetManifest, resolvedEnvironment, options.stackName);
await publisher.buildEntry(asset);
if (publisher.hasFailures) {
throw new Error(`Failed to build asset ${asset.id}`);
throw new ToolkitError(`Failed to build asset ${asset.id}`);

Check warning on line 702 in packages/aws-cdk/lib/api/deployments.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/api/deployments.ts#L702

Added line #L702 was not covered by tests
}
}

Expand All @@ -717,7 +718,7 @@
const publisher = this.cachedPublisher(assetManifest, stackEnv, options.stackName);
await publisher.publishEntry(asset, { allowCrossAccount: await this.allowCrossAccountAssetPublishingForEnv(options.stack) });
if (publisher.hasFailures) {
throw new Error(`Failed to publish asset ${asset.id}`);
throw new ToolkitError(`Failed to publish asset ${asset.id}`);

Check warning on line 721 in packages/aws-cdk/lib/api/deployments.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/api/deployments.ts#L721

Added line #L721 was not covered by tests
}
}

Expand Down Expand Up @@ -756,7 +757,7 @@
try {
await envResources.validateVersion(requiresBootstrapStackVersion, bootstrapStackVersionSsmParameter);
} catch (e: any) {
throw new Error(`${stackName}: ${formatErrorMessage(e)}`);
throw new ToolkitError(`${stackName}: ${formatErrorMessage(e)}`);
}
}

Expand Down
9 changes: 5 additions & 4 deletions packages/aws-cdk/lib/api/environment-access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import { EnvironmentResources, EnvironmentResourcesRegistry } from './environment-resources';
import { Mode } from './plugin/mode';
import { replaceEnvPlaceholders, StringWithoutPlaceholders } from './util/placeholders';
import { ToolkitError } from '../toolkit/error';
import { formatErrorMessage } from '../util/error';

/**
Expand Down Expand Up @@ -87,7 +88,7 @@
*/
public async accessStackForLookup(stack: cxapi.CloudFormationStackArtifact): Promise<TargetEnvironment> {
if (!stack.environment) {
throw new Error(`The stack ${stack.displayName} does not have an environment`);
throw new ToolkitError(`The stack ${stack.displayName} does not have an environment`);

Check warning on line 91 in packages/aws-cdk/lib/api/environment-access.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/api/environment-access.ts#L91

Added line #L91 was not covered by tests
}

const lookupEnv = await this.prepareSdk({
Expand All @@ -102,7 +103,7 @@
if (lookupEnv.didAssumeRole && stack.lookupRole?.bootstrapStackVersionSsmParameter && stack.lookupRole.requiresBootstrapStackVersion) {
const version = await lookupEnv.resources.versionFromSsmParameter(stack.lookupRole.bootstrapStackVersionSsmParameter);
if (version < stack.lookupRole.requiresBootstrapStackVersion) {
throw new Error(`Bootstrap stack version '${stack.lookupRole.requiresBootstrapStackVersion}' is required, found version '${version}'. To get rid of this error, please upgrade to bootstrap version >= ${stack.lookupRole.requiresBootstrapStackVersion}`);
throw new ToolkitError(`Bootstrap stack version '${stack.lookupRole.requiresBootstrapStackVersion}' is required, found version '${version}'. To get rid of this error, please upgrade to bootstrap version >= ${stack.lookupRole.requiresBootstrapStackVersion}`);
}
}
if (lookupEnv.isFallbackCredentials) {
Expand All @@ -125,7 +126,7 @@
*/
public async accessStackForLookupBestEffort(stack: cxapi.CloudFormationStackArtifact): Promise<TargetEnvironment> {
if (!stack.environment) {
throw new Error(`The stack ${stack.displayName} does not have an environment`);
throw new ToolkitError(`The stack ${stack.displayName} does not have an environment`);

Check warning on line 129 in packages/aws-cdk/lib/api/environment-access.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/api/environment-access.ts#L129

Added line #L129 was not covered by tests
}

try {
Expand All @@ -147,7 +148,7 @@
*/
private async accessStackForStackOperations(stack: cxapi.CloudFormationStackArtifact, mode: Mode): Promise<TargetEnvironment> {
if (!stack.environment) {
throw new Error(`The stack ${stack.displayName} does not have an environment`);
throw new ToolkitError(`The stack ${stack.displayName} does not have an environment`);

Check warning on line 151 in packages/aws-cdk/lib/api/environment-access.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/api/environment-access.ts#L151

Added line #L151 was not covered by tests
}

return this.prepareSdk({
Expand Down
15 changes: 8 additions & 7 deletions packages/aws-cdk/lib/api/environment-resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { type EcrRepositoryInfo, ToolkitInfo } from './toolkit-info';
import { debug, warning } from '../logging';
import { Notices } from '../notices';
import { ToolkitError } from '../toolkit/error';
import { formatErrorMessage } from '../util/error';

/**
Expand Down Expand Up @@ -101,7 +102,7 @@
return;
}

throw new Error(
throw new ToolkitError(
`This CDK deployment requires bootstrap stack version '${expectedVersion}', but during the confirmation via SSM parameter ${ssmParameterName} the following error occurred: ${e}`,
);
}
Expand All @@ -119,7 +120,7 @@
notices.addBootstrappedEnvironment({ bootstrapStackVersion: version, environment });
}
if (defExpectedVersion > version) {
throw new Error(
throw new ToolkitError(
`This CDK deployment requires bootstrap stack version '${expectedVersion}', found '${version}'. Please run 'cdk bootstrap'.`,
);
}
Expand All @@ -142,14 +143,14 @@

const asNumber = parseInt(`${result.Parameter?.Value}`, 10);
if (isNaN(asNumber)) {
throw new Error(`SSM parameter ${parameterName} not a number: ${result.Parameter?.Value}`);
throw new ToolkitError(`SSM parameter ${parameterName} not a number: ${result.Parameter?.Value}`);

Check warning on line 146 in packages/aws-cdk/lib/api/environment-resources.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/api/environment-resources.ts#L146

Added line #L146 was not covered by tests
}

this.cache.ssmParameters.set(parameterName, asNumber);
return asNumber;
} catch (e: any) {
if (e.name === 'ParameterNotFound') {
throw new Error(
throw new ToolkitError(
`SSM parameter ${parameterName} not found. Has the environment been bootstrapped? Please run \'cdk bootstrap\' (see https://docs.aws.amazon.com/cdk/latest/guide/bootstrapping.html)`,
);
}
Expand All @@ -159,7 +160,7 @@

public async prepareEcrRepository(repositoryName: string): Promise<EcrRepositoryInfo> {
if (!this.sdk) {
throw new Error('ToolkitInfo needs to have been initialized with an sdk to call prepareEcrRepository');
throw new ToolkitError('ToolkitInfo needs to have been initialized with an sdk to call prepareEcrRepository');

Check warning on line 163 in packages/aws-cdk/lib/api/environment-resources.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/api/environment-resources.ts#L163

Added line #L163 was not covered by tests
}
const ecr = this.sdk.ecr();

Expand Down Expand Up @@ -188,7 +189,7 @@
});
const repositoryUri = response.repository?.repositoryUri;
if (!repositoryUri) {
throw new Error(`CreateRepository did not return a repository URI for ${repositoryUri}`);
throw new ToolkitError(`CreateRepository did not return a repository URI for ${repositoryUri}`);

Check warning on line 192 in packages/aws-cdk/lib/api/environment-resources.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/api/environment-resources.ts#L192

Added line #L192 was not covered by tests
}

// configure image scanning on push (helps in identifying software vulnerabilities, no additional charge)
Expand All @@ -211,7 +212,7 @@
* Look up the toolkit for a given environment, using a given SDK
*/
public async lookupToolkit(): Promise<ToolkitInfo> {
throw new Error(
throw new ToolkitError(
'Trying to perform an operation that requires a bootstrap stack; you should not see this error, this is a bug in the CDK CLI.',
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Export, ListExportsCommandOutput, StackResourceSummary } from '@aws-sdk/client-cloudformation';
import type { SDK } from './aws-auth';
import type { NestedStackTemplates } from './nested-stack-helpers';
import { ToolkitError } from '../toolkit/error';

export interface ListStackResources {
listStackResources(): Promise<StackResourceSummary[]>;
Expand Down Expand Up @@ -556,7 +557,7 @@

async function asyncGlobalReplace(str: string, regex: RegExp, cb: (x: string) => Promise<string>): Promise<string> {
if (!regex.global) {
throw new Error('Regex must be created with /g flag');
throw new ToolkitError('Regex must be created with /g flag');

Check warning on line 560 in packages/aws-cdk/lib/api/evaluate-cloudformation-template.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/api/evaluate-cloudformation-template.ts#L560

Added line #L560 was not covered by tests
}

const ret = new Array<string>();
Expand Down
3 changes: 2 additions & 1 deletion packages/aws-cdk/lib/api/hotswap-deployments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import { NestedStackTemplates, loadCurrentTemplateWithNestedStacks } from './nested-stack-helpers';
import { Mode } from './plugin/mode';
import { CloudFormationStack } from './util/cloudformation';
import { ToolkitError } from '../toolkit/error';
import { formatErrorMessage } from '../util/error';

// Must use a require() otherwise esbuild complains about calling a namespace
Expand Down Expand Up @@ -425,7 +426,7 @@
} catch (e: any) {
if (e.name === 'TimeoutError' || e.name === 'AbortError') {
const result: WaiterResult = JSON.parse(formatErrorMessage(e));
const error = new Error([
const error = new ToolkitError([

Check warning on line 429 in packages/aws-cdk/lib/api/hotswap-deployments.ts

View check run for this annotation

Codecov / codecov/patch

packages/aws-cdk/lib/api/hotswap-deployments.ts#L429

Added line #L429 was not covered by tests
`Resource is not in the expected state due to waiter status: ${result.state}`,
result.reason ? `${result.reason}.` : '',
].join('. '));
Expand Down
Loading
Loading