Skip to content

Commit ec4b91b

Browse files
authored
chore(toolkit): all error messages must have a code (#33317)
### Reason for this change We want errors to always have a defined non-default message code. This way an integrator can target errors individually. ### Description of changes Change the helper method for error messages to require a code. ### Describe any new or updated permissions being added n/a ### Description of how you validated changes n/a this is a refactor, enforcing a new rule at compile time ### 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 34821f2 commit ec4b91b

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

packages/@aws-cdk/toolkit/lib/api/io/private/codes.ts

+8
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,21 @@ export const CODES = {
1414
CDK_TOOLKIT_I5060: 'Confirm deploy security sensitive changes',
1515
CDK_TOOLKIT_I5900: 'Deployment results on success',
1616

17+
CDK_TOOLKIT_E5001: 'No stacks found',
18+
1719
// Rollback
1820
CDK_TOOLKIT_I6000: 'Provides rollback times',
1921

22+
CDK_TOOLKIT_E6001: 'No stacks found',
23+
CDK_TOOLKIT_E6900: 'Rollback failed',
24+
2025
// Destroy
2126
CDK_TOOLKIT_I7000: 'Provides destroy times',
2227
CDK_TOOLKIT_I7010: 'Confirm destroy stacks',
2328

29+
CDK_TOOLKIT_E7010: 'Action was aborted due to negative confirmation of request',
30+
CDK_TOOLKIT_E7900: 'Stack deletion failed',
31+
2432
// Assembly codes
2533
CDK_ASSEMBLY_I0042: 'Writing updated context',
2634
CDK_ASSEMBLY_I0241: 'Fetching missing context',

packages/@aws-cdk/toolkit/lib/api/io/private/messages.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ export const prompt = <T, U>(code: VALID_CODE, message: string, defaultResponse:
6565

6666
/**
6767
* Creates an error level message.
68+
* Errors must always have a unique code.
6869
*/
69-
export const error = <T>(message: string, code?: VALID_CODE, payload?: T) => {
70+
export const error = <T>(message: string, code: VALID_CODE, payload?: T) => {
7071
return formatMessage({
7172
level: 'error',
7273
code,

packages/@aws-cdk/toolkit/lib/toolkit/toolkit.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ export class Toolkit extends CloudAssemblySourceBuilder implements AsyncDisposab
243243
const synthDuration = await synthTimer.endAs(ioHost, 'synth');
244244

245245
if (stackCollection.stackCount === 0) {
246-
await ioHost.notify(error('This app contains no stacks'));
246+
await ioHost.notify(error('This app contains no stacks', 'CDK_TOOLKIT_E5001'));
247247
return;
248248
}
249249

@@ -648,7 +648,7 @@ export class Toolkit extends CloudAssemblySourceBuilder implements AsyncDisposab
648648
await synthTimer.endAs(ioHost, 'synth');
649649

650650
if (stacks.stackCount === 0) {
651-
await ioHost.notify(error('No stacks selected'));
651+
await ioHost.notify(error('No stacks selected', 'CDK_TOOLKIT_E6001'));
652652
return;
653653
}
654654

@@ -672,7 +672,7 @@ export class Toolkit extends CloudAssemblySourceBuilder implements AsyncDisposab
672672
}
673673
await rollbackTimer.endAs(ioHost, 'rollback');
674674
} catch (e: any) {
675-
await ioHost.notify(error(`\n ❌ ${chalk.bold(stack.displayName)} failed: ${formatErrorMessage(e)}`));
675+
await ioHost.notify(error(`\n ❌ ${chalk.bold(stack.displayName)} failed: ${formatErrorMessage(e)}`, 'CDK_TOOLKIT_E6900'));
676676
throw new ToolkitError('Rollback failed (use --force to orphan failing resources)');
677677
}
678678
}
@@ -705,7 +705,7 @@ export class Toolkit extends CloudAssemblySourceBuilder implements AsyncDisposab
705705
const question = `Are you sure you want to delete: ${chalk.red(stacks.hierarchicalIds.join(', '))}`;
706706
const confirmed = await ioHost.requestResponse(confirm('CDK_TOOLKIT_I7010', question, motivation, true));
707707
if (!confirmed) {
708-
return ioHost.notify(error('Aborted by user'));
708+
return ioHost.notify(error('Aborted by user', 'CDK_TOOLKIT_E7010'));
709709
}
710710

711711
const destroyTimer = Timer.start();
@@ -722,7 +722,7 @@ export class Toolkit extends CloudAssemblySourceBuilder implements AsyncDisposab
722722
});
723723
await ioHost.notify(success(`\n ✅ ${chalk.blue(stack.displayName)}: ${action}ed`));
724724
} catch (e) {
725-
await ioHost.notify(error(`\n ❌ ${chalk.blue(stack.displayName)}: ${action} failed ${e}`));
725+
await ioHost.notify(error(`\n ❌ ${chalk.blue(stack.displayName)}: ${action} failed ${e}`, 'CDK_TOOLKIT_E7900'));
726726
throw e;
727727
}
728728
}

0 commit comments

Comments
 (0)