Skip to content

Commit

Permalink
error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
kaizencc committed Jan 27, 2025
1 parent 7ca354c commit 17f52de
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
39 changes: 39 additions & 0 deletions packages/@aws-cdk/toolkit/lib/api/io/private/codes.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,54 @@
import { ToolkitError } from '../../errors';
import { IoMessageCode } from '../io-message';

export const CODES = {
// Default codes -- all 0000 codes
CDK_TOOLKIT_I0000: 'Default toolkit info code',
CDK_TOOLKIT_E0000: 'Default toolkit error code',
CDK_TOOLKIT_WOOOO: 'Default toolkit warning code',
CDK_SDK_I0000: 'Default sdk info code',
CDK_SDK_E0000: 'Default sdk error code',
CDK_SDK_WOOOO: 'Default sdk warning code',
CDK_ASSETS_I0000: 'Default assets info code',
CDK_ASSETS_E0000: 'Default assets error code',
CDK_ASSETS_WOOOO: 'Default assets warning code',
CDK_ASSEMBLY_I0000: 'Default assembly info code',
CDK_ASSEMBLY_E0000: 'Default assembly error code',
CDK_ASSEMBLY_WOOOO: 'Default assembly warning code',

// Toolkit Info codes
CDK_TOOLKIT_I0001: 'Display stack data',
CDK_TOOLKIT_I0002: 'Successfully deployed stacks',
CDK_TOOLKIT_I5001: 'Display synthesis times',
CDK_TOOLKIT_I5050: 'Confirm rollback',
CDK_TOOLKIT_I5060: 'Confirm deploy security sensitive changes',
CDK_TOOLKIT_I7010: 'Confirm destroy stacks',

// Toolkit Warning codes

// Toolkit Error codes

// Assembly Info codes
CDK_ASSEMBLY_I0042: 'Writing updated context',
CDK_ASSEMBLY_I0241: 'Fetching missing context',

// Assembly Warning codes

// Assembly Error codes
CDK_ASSEMBLY_E1111: 'Incompatible CDK CLI version. Upgrade needed.',
};

// If we give CODES a type with key: IoMessageCode,
// this dynamically generated type will generalize to allow all IoMessageCodes.
// Instead, we will validate that VALID_CODE must be IoMessageCode with the '&'.
export type VALID_CODE = keyof typeof CODES & IoMessageCode;

/**
* Verifies that the input is a valid code, and returns a ToolkitError if not.
*/
export function isValidCode(code: IoMessageCode): VALID_CODE {
if (code in CODES) {
return code as VALID_CODE;
}
throw new ToolkitError(`Invalid message code: ${code}`);
}
11 changes: 6 additions & 5 deletions packages/@aws-cdk/toolkit/lib/api/io/private/messages.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as chalk from 'chalk';
import type { IoMessageCode, IoMessageCodeCategory, IoMessageLevel } from '../io-message';
import type { VALID_CODE } from './codes';
import type { IoMessageCodeCategory, IoMessageLevel } from '../io-message';
import { isValidCode as verifyValidCode, type VALID_CODE } from './codes';
import type { ActionLessMessage, ActionLessRequest, Optional, SimplifiedMessage } from './types';

/**
Expand All @@ -19,13 +19,14 @@ export function formatMessage<T>(msg: Optional<SimplifiedMessage<T>, 'code'>, ca
}

/**
* Build a message code from level and category
* Build a message code from level and category. The code must be valid for this function to pass.
* Otherwise it returns a ToolkitError.
*/
export function messageCode(level: IoMessageLevel, category: IoMessageCodeCategory = 'TOOLKIT', number?: `${number}${number}${number}${number}`): IoMessageCode {
export function messageCode(level: IoMessageLevel, category: IoMessageCodeCategory = 'TOOLKIT', number?: `${number}${number}${number}${number}`): VALID_CODE {
const levelIndicator = level === 'error' ? 'E' :
level === 'warn' ? 'W' :
'I';
return `CDK_${category}_${levelIndicator}${number ?? '0000'}`;
return verifyValidCode(`CDK_${category}_${levelIndicator}${number ?? '0000'}`);
}

/**
Expand Down

0 comments on commit 17f52de

Please sign in to comment.