Skip to content

Commit

Permalink
feat: throw ValidationError instead of untyped errors in L1s (#33032)
Browse files Browse the repository at this point in the history
### 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*
  • Loading branch information
mrgrain authored Jan 27, 2025
1 parent 918a3a8 commit 1b666db
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions tools/@aws-cdk/spec2cdk/lib/cdk/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export class AstBuilder<T extends Module> {
CDK_CORE.import(this.module, 'cdk', { fromLocation: props.importLocations?.core });
CONSTRUCTS.import(this.module, 'constructs');
CDK_CORE.helpers.import(this.module, 'cfn_parse', { fromLocation: props.importLocations?.coreHelpers });
CDK_CORE.errors.import(this.module, 'cdk_errors', { fromLocation: props.importLocations?.coreErrors });
}

public addResource(resource: Resource) {
Expand Down
15 changes: 14 additions & 1 deletion tools/@aws-cdk/spec2cdk/lib/cdk/cdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ export interface ModuleImportLocations {
* @default 'aws-cdk-lib/core/lib/helpers-internal'
*/
readonly coreHelpers?: string;

/**
* The import name used to import core errors module
* @default 'aws-cdk-lib/core/lib/errors'
*/
readonly coreErrors?: string;
/**
* The import name used to import the CloudWatch module
*
Expand All @@ -22,6 +26,7 @@ export interface ModuleImportLocations {

export class CdkCore extends ExternalModule {
public readonly helpers = new CdkInternalHelpers(this);
public readonly errors = new CdkErrors(this);

public readonly CfnResource = Type.fromName(this, 'CfnResource');
public readonly Resource = $T(Type.fromName(this, 'Resource'));
Expand Down Expand Up @@ -93,6 +98,14 @@ export class CdkInternalHelpers extends ExternalModule {
}
}

export class CdkErrors extends ExternalModule {
public readonly ValidationError = Type.fromName(this, 'ValidationError');

constructor(parent: CdkCore) {
super(`${parent.fqn}/core/lib/errors`);
}
}

export class Constructs extends ExternalModule {
public readonly Construct = Type.fromName(this, 'Construct');
public readonly IConstruct = Type.fromName(this, 'IConstruct');
Expand Down
2 changes: 1 addition & 1 deletion tools/@aws-cdk/spec2cdk/lib/cdk/resource-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export class ResourceClass extends ClassType {
stmt.constVar(propsResult, reverseMapper.call(resourceProperties)),
stmt
.if_(CDK_CORE.isResolvableObject(propsResult.value))
.then(stmt.block(stmt.throw_(Type.ambient('Error').newInstance(expr.lit('Unexpected IResolvable'))))),
.then(stmt.block(stmt.throw_(CDK_CORE.errors.ValidationError.newInstance(expr.lit('Unexpected IResolvable'), scope)))),
stmt.constVar(ret, this.newInstance(scope, id, propsResult.value)),
);

Expand Down
6 changes: 5 additions & 1 deletion tools/@aws-cdk/spec2cdk/lib/cfn2ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as fs from 'fs-extra';
import * as pLimit from 'p-limit';
import * as pkglint from './pkglint';
import { CodeGeneratorOptions, GenerateAllOptions, ModuleMap } from './types';
import type { ModuleImportLocations } from '../cdk/cdk';
import { generate as generateModules } from '../generate';
import { log } from '../util';

Expand Down Expand Up @@ -56,6 +57,7 @@ export default async function generate(
importLocations: {
core: coreImport,
coreHelpers: `${coreImport}/${coreImport === '.' ? '' : 'lib/'}helpers-internal`,
coreErrors: `${coreImport}/${coreImport === '.' ? '' : 'lib/'}errors`,
},
},
);
Expand Down Expand Up @@ -132,9 +134,10 @@ export async function generateAll(
}

const coreModule = 'core';
const coreImportLocations = {
const coreImportLocations: ModuleImportLocations = {
core: '.',
coreHelpers: './helpers-internal',
coreErrors: './errors',
};

const generated = await generateModules(
Expand All @@ -159,6 +162,7 @@ export async function generateAll(
importLocations: {
core: options.coreImport,
coreHelpers: `${options.coreImport}/lib/helpers-internal`,
coreErrors: `${options.coreImport}/lib/errors`,
cloudwatch: options.cloudwatchImport,
},
},
Expand Down

0 comments on commit 1b666db

Please sign in to comment.