Skip to content

Commit

Permalink
chore(toolkit): all error messages must have a code (#33317)
Browse files Browse the repository at this point in the history
### 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*
  • Loading branch information
mrgrain authored Feb 7, 2025
1 parent 34821f2 commit ec4b91b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
8 changes: 8 additions & 0 deletions packages/@aws-cdk/toolkit/lib/api/io/private/codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@ export const CODES = {
CDK_TOOLKIT_I5060: 'Confirm deploy security sensitive changes',
CDK_TOOLKIT_I5900: 'Deployment results on success',

CDK_TOOLKIT_E5001: 'No stacks found',

// Rollback
CDK_TOOLKIT_I6000: 'Provides rollback times',

CDK_TOOLKIT_E6001: 'No stacks found',
CDK_TOOLKIT_E6900: 'Rollback failed',

// Destroy
CDK_TOOLKIT_I7000: 'Provides destroy times',
CDK_TOOLKIT_I7010: 'Confirm destroy stacks',

CDK_TOOLKIT_E7010: 'Action was aborted due to negative confirmation of request',
CDK_TOOLKIT_E7900: 'Stack deletion failed',

// Assembly codes
CDK_ASSEMBLY_I0042: 'Writing updated context',
CDK_ASSEMBLY_I0241: 'Fetching missing context',
Expand Down
3 changes: 2 additions & 1 deletion packages/@aws-cdk/toolkit/lib/api/io/private/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ export const prompt = <T, U>(code: VALID_CODE, message: string, defaultResponse:

/**
* Creates an error level message.
* Errors must always have a unique code.
*/
export const error = <T>(message: string, code?: VALID_CODE, payload?: T) => {
export const error = <T>(message: string, code: VALID_CODE, payload?: T) => {
return formatMessage({
level: 'error',
code,
Expand Down
10 changes: 5 additions & 5 deletions packages/@aws-cdk/toolkit/lib/toolkit/toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ export class Toolkit extends CloudAssemblySourceBuilder implements AsyncDisposab
const synthDuration = await synthTimer.endAs(ioHost, 'synth');

if (stackCollection.stackCount === 0) {
await ioHost.notify(error('This app contains no stacks'));
await ioHost.notify(error('This app contains no stacks', 'CDK_TOOLKIT_E5001'));
return;
}

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

if (stacks.stackCount === 0) {
await ioHost.notify(error('No stacks selected'));
await ioHost.notify(error('No stacks selected', 'CDK_TOOLKIT_E6001'));
return;
}

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

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

0 comments on commit ec4b91b

Please sign in to comment.