Skip to content

Commit 1b666db

Browse files
authored
feat: throw ValidationError instead of untyped errors in L1s (#33032)
### Issue All L1s for #32569 ### Description of changes Updated the codegen to throw the correct error. Instead of ```ts throw new Error("Unexpected IResolvable"); ``` we now throw ```ts throw new errors.ValidationError("Unexpected IResolvable", scope); ``` ### Describe any new or updated permissions being added n/a ### Description of how you validated changes Existing tests. Exemptions granted as this is basically a refactor of existing code. ### 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 918a3a8 commit 1b666db

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

tools/@aws-cdk/spec2cdk/lib/cdk/ast.ts

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export class AstBuilder<T extends Module> {
9090
CDK_CORE.import(this.module, 'cdk', { fromLocation: props.importLocations?.core });
9191
CONSTRUCTS.import(this.module, 'constructs');
9292
CDK_CORE.helpers.import(this.module, 'cfn_parse', { fromLocation: props.importLocations?.coreHelpers });
93+
CDK_CORE.errors.import(this.module, 'cdk_errors', { fromLocation: props.importLocations?.coreErrors });
9394
}
9495

9596
public addResource(resource: Resource) {

tools/@aws-cdk/spec2cdk/lib/cdk/cdk.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ export interface ModuleImportLocations {
1111
* @default 'aws-cdk-lib/core/lib/helpers-internal'
1212
*/
1313
readonly coreHelpers?: string;
14-
14+
/**
15+
* The import name used to import core errors module
16+
* @default 'aws-cdk-lib/core/lib/errors'
17+
*/
18+
readonly coreErrors?: string;
1519
/**
1620
* The import name used to import the CloudWatch module
1721
*
@@ -22,6 +26,7 @@ export interface ModuleImportLocations {
2226

2327
export class CdkCore extends ExternalModule {
2428
public readonly helpers = new CdkInternalHelpers(this);
29+
public readonly errors = new CdkErrors(this);
2530

2631
public readonly CfnResource = Type.fromName(this, 'CfnResource');
2732
public readonly Resource = $T(Type.fromName(this, 'Resource'));
@@ -93,6 +98,14 @@ export class CdkInternalHelpers extends ExternalModule {
9398
}
9499
}
95100

101+
export class CdkErrors extends ExternalModule {
102+
public readonly ValidationError = Type.fromName(this, 'ValidationError');
103+
104+
constructor(parent: CdkCore) {
105+
super(`${parent.fqn}/core/lib/errors`);
106+
}
107+
}
108+
96109
export class Constructs extends ExternalModule {
97110
public readonly Construct = Type.fromName(this, 'Construct');
98111
public readonly IConstruct = Type.fromName(this, 'IConstruct');

tools/@aws-cdk/spec2cdk/lib/cdk/resource-class.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ export class ResourceClass extends ClassType {
192192
stmt.constVar(propsResult, reverseMapper.call(resourceProperties)),
193193
stmt
194194
.if_(CDK_CORE.isResolvableObject(propsResult.value))
195-
.then(stmt.block(stmt.throw_(Type.ambient('Error').newInstance(expr.lit('Unexpected IResolvable'))))),
195+
.then(stmt.block(stmt.throw_(CDK_CORE.errors.ValidationError.newInstance(expr.lit('Unexpected IResolvable'), scope)))),
196196
stmt.constVar(ret, this.newInstance(scope, id, propsResult.value)),
197197
);
198198

tools/@aws-cdk/spec2cdk/lib/cfn2ts/index.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as fs from 'fs-extra';
44
import * as pLimit from 'p-limit';
55
import * as pkglint from './pkglint';
66
import { CodeGeneratorOptions, GenerateAllOptions, ModuleMap } from './types';
7+
import type { ModuleImportLocations } from '../cdk/cdk';
78
import { generate as generateModules } from '../generate';
89
import { log } from '../util';
910

@@ -56,6 +57,7 @@ export default async function generate(
5657
importLocations: {
5758
core: coreImport,
5859
coreHelpers: `${coreImport}/${coreImport === '.' ? '' : 'lib/'}helpers-internal`,
60+
coreErrors: `${coreImport}/${coreImport === '.' ? '' : 'lib/'}errors`,
5961
},
6062
},
6163
);
@@ -132,9 +134,10 @@ export async function generateAll(
132134
}
133135

134136
const coreModule = 'core';
135-
const coreImportLocations = {
137+
const coreImportLocations: ModuleImportLocations = {
136138
core: '.',
137139
coreHelpers: './helpers-internal',
140+
coreErrors: './errors',
138141
};
139142

140143
const generated = await generateModules(
@@ -159,6 +162,7 @@ export async function generateAll(
159162
importLocations: {
160163
core: options.coreImport,
161164
coreHelpers: `${options.coreImport}/lib/helpers-internal`,
165+
coreErrors: `${options.coreImport}/lib/errors`,
162166
cloudwatch: options.cloudwatchImport,
163167
},
164168
},

0 commit comments

Comments
 (0)