Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: throw ValidationError instead of untyped errors in L1s #33032

Merged
merged 2 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading