From 26aa905f48e96eac3deb510dd5dd48596dce8aef Mon Sep 17 00:00:00 2001 From: Momo Kornher Date: Fri, 24 Jan 2025 15:05:37 +0000 Subject: [PATCH] feat(amplify-alpha): throw `ValidationError` instead of untyped errors --- packages/@aws-cdk/aws-amplify-alpha/.eslintrc.js | 7 +++++++ packages/@aws-cdk/aws-amplify-alpha/lib/domain.ts | 6 +++--- packages/aws-cdk-lib/.eslintrc.js | 2 ++ packages/aws-cdk-lib/core/lib/errors.ts | 4 ++++ packages/aws-cdk-lib/core/lib/index.ts | 2 +- 5 files changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/@aws-cdk/aws-amplify-alpha/.eslintrc.js b/packages/@aws-cdk/aws-amplify-alpha/.eslintrc.js index b284f20df26e9..0349bd1a34c53 100644 --- a/packages/@aws-cdk/aws-amplify-alpha/.eslintrc.js +++ b/packages/@aws-cdk/aws-amplify-alpha/.eslintrc.js @@ -4,5 +4,12 @@ baseConfig.parserOptions.project = __dirname + '/tsconfig.json'; baseConfig.rules['import/no-extraneous-dependencies'] = ['error', { devDependencies: true, peerDependencies: true } ]; baseConfig.rules['import/order'] = 'off'; baseConfig.rules['@aws-cdk/invalid-cfn-imports'] = 'off'; +baseConfig.rules['@cdklabs/no-throw-default-error'] = ['error']; +baseConfig.overrides.push({ + files: ["./test/**"], + rules: { + "@cdklabs/no-throw-default-error": "off", + }, +}); module.exports = baseConfig; diff --git a/packages/@aws-cdk/aws-amplify-alpha/lib/domain.ts b/packages/@aws-cdk/aws-amplify-alpha/lib/domain.ts index 5dcbe874990da..25f0a6daa4fcc 100644 --- a/packages/@aws-cdk/aws-amplify-alpha/lib/domain.ts +++ b/packages/@aws-cdk/aws-amplify-alpha/lib/domain.ts @@ -1,6 +1,6 @@ import * as acm from 'aws-cdk-lib/aws-certificatemanager'; import * as iam from 'aws-cdk-lib/aws-iam'; -import { Lazy, Resource, IResolvable, Token } from 'aws-cdk-lib/core'; +import { Lazy, Resource, IResolvable, Token, ValidationError } from 'aws-cdk-lib/core'; import { Construct } from 'constructs'; import { CfnDomain } from 'aws-cdk-lib/aws-amplify'; import { IApp } from './app'; @@ -131,10 +131,10 @@ export class Domain extends Resource { const domainName = props.domainName || id; if (!Token.isUnresolved(domainName) && domainName.length > 255) { - throw new Error(`Domain name must be 255 characters or less, got: ${domainName.length} characters.`); + throw new ValidationError(`Domain name must be 255 characters or less, got: ${domainName.length} characters.`, this); } if (!Token.isUnresolved(domainName) && !/^(((?!-)[A-Za-z0-9-]{0,62}[A-Za-z0-9])\.)+((?!-)[A-Za-z0-9-]{1,62}[A-Za-z0-9])(\.)?$/.test(domainName)) { - throw new Error(`Domain name must be a valid hostname, got: ${domainName}.`); + throw new ValidationError(`Domain name must be a valid hostname, got: ${domainName}.`, this); } const domain = new CfnDomain(this, 'Resource', { diff --git a/packages/aws-cdk-lib/.eslintrc.js b/packages/aws-cdk-lib/.eslintrc.js index 83d55151fca9d..331262e7d4b6b 100644 --- a/packages/aws-cdk-lib/.eslintrc.js +++ b/packages/aws-cdk-lib/.eslintrc.js @@ -18,6 +18,8 @@ baseConfig.rules['import/no-extraneous-dependencies'] = [ const enableNoThrowDefaultErrorIn = [ 'aws-apigatewayv2-integrations', 'aws-apigatewayv2-authorizers', + 'aws-amplify', + 'aws-amplifyuibuilder', 'aws-lambda', 'aws-rds', 'aws-s3', diff --git a/packages/aws-cdk-lib/core/lib/errors.ts b/packages/aws-cdk-lib/core/lib/errors.ts index 2d6a310e724c4..2764c7adc89bb 100644 --- a/packages/aws-cdk-lib/core/lib/errors.ts +++ b/packages/aws-cdk-lib/core/lib/errors.ts @@ -142,6 +142,8 @@ abstract class ConstructError extends Error { * * A ValidationError is always attached to a Construct scope. To a user, the error will present with additional * information on the construct that caused the validation to fail. + * + * @internal */ export class ValidationError extends ConstructError { public get type(): 'validation' { @@ -162,6 +164,8 @@ export class ValidationError extends ConstructError { * * To a User, these errors still present themselves as a "ValidationError". * However they do not contain any information about the location in the construct tree. + * + * @internal */ export class UnscopedValidationError extends ConstructError { public get type(): 'validation' { diff --git a/packages/aws-cdk-lib/core/lib/index.ts b/packages/aws-cdk-lib/core/lib/index.ts index 9fb4041a136a0..de8f05498ff9a 100644 --- a/packages/aws-cdk-lib/core/lib/index.ts +++ b/packages/aws-cdk-lib/core/lib/index.ts @@ -35,7 +35,7 @@ export * from './expiration'; export * from './size'; export * from './stack-trace'; export { Element } from './deps'; -export { Errors } from './errors'; +export * from './errors'; export * from './app'; export * from './context-provider';