From 1550647ec4290a7140b71552a2688e7af266c3d0 Mon Sep 17 00:00:00 2001 From: Momo Kornher Date: Tue, 21 Jan 2025 13:48:14 +0000 Subject: [PATCH] feat: use `ValidationError` in L1s --- tools/@aws-cdk/spec2cdk/lib/cdk/ast.ts | 1 + tools/@aws-cdk/spec2cdk/lib/cdk/cdk.ts | 15 ++++++++++++++- tools/@aws-cdk/spec2cdk/lib/cdk/resource-class.ts | 2 +- tools/@aws-cdk/spec2cdk/lib/cfn2ts/index.ts | 6 +++++- 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/tools/@aws-cdk/spec2cdk/lib/cdk/ast.ts b/tools/@aws-cdk/spec2cdk/lib/cdk/ast.ts index 654c99e9c6d3e..a321bd4ef50a3 100644 --- a/tools/@aws-cdk/spec2cdk/lib/cdk/ast.ts +++ b/tools/@aws-cdk/spec2cdk/lib/cdk/ast.ts @@ -90,6 +90,7 @@ export class AstBuilder { 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) { diff --git a/tools/@aws-cdk/spec2cdk/lib/cdk/cdk.ts b/tools/@aws-cdk/spec2cdk/lib/cdk/cdk.ts index 3c4a4de33df35..195a844ac2bc2 100644 --- a/tools/@aws-cdk/spec2cdk/lib/cdk/cdk.ts +++ b/tools/@aws-cdk/spec2cdk/lib/cdk/cdk.ts @@ -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 * @@ -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')); @@ -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'); diff --git a/tools/@aws-cdk/spec2cdk/lib/cdk/resource-class.ts b/tools/@aws-cdk/spec2cdk/lib/cdk/resource-class.ts index d31ae552f096e..7890e6efa5b0a 100644 --- a/tools/@aws-cdk/spec2cdk/lib/cdk/resource-class.ts +++ b/tools/@aws-cdk/spec2cdk/lib/cdk/resource-class.ts @@ -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)), ); diff --git a/tools/@aws-cdk/spec2cdk/lib/cfn2ts/index.ts b/tools/@aws-cdk/spec2cdk/lib/cfn2ts/index.ts index 4d2e5605b05fc..8f278c10fe5e7 100644 --- a/tools/@aws-cdk/spec2cdk/lib/cfn2ts/index.ts +++ b/tools/@aws-cdk/spec2cdk/lib/cfn2ts/index.ts @@ -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'; @@ -56,6 +57,7 @@ export default async function generate( importLocations: { core: coreImport, coreHelpers: `${coreImport}/${coreImport === '.' ? '' : 'lib/'}helpers-internal`, + coreErrors: `${coreImport}/${coreImport === '.' ? '' : 'lib/'}errors`, }, }, ); @@ -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( @@ -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, }, },