From d68be459fb7bceb19362e853cce818f04e9e543d Mon Sep 17 00:00:00 2001 From: Yuki Matsuda <13781813+mazyu36@users.noreply.github.com> Date: Tue, 22 Apr 2025 11:09:33 +0900 Subject: [PATCH] feat(msk): throw ValidationError instead of untyped errors --- packages/@aws-cdk/aws-msk-alpha/.eslintrc.js | 7 +++++++ .../@aws-cdk/aws-msk-alpha/lib/cluster.ts | 20 ++++++++----------- .../aws-msk-alpha/lib/serverless-cluster.ts | 10 ++++------ 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/packages/@aws-cdk/aws-msk-alpha/.eslintrc.js b/packages/@aws-cdk/aws-msk-alpha/.eslintrc.js index b284f20df26e9..0349bd1a34c53 100644 --- a/packages/@aws-cdk/aws-msk-alpha/.eslintrc.js +++ b/packages/@aws-cdk/aws-msk-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-msk-alpha/lib/cluster.ts b/packages/@aws-cdk/aws-msk-alpha/lib/cluster.ts index d7cbee9af2f64..4629792a98927 100644 --- a/packages/@aws-cdk/aws-msk-alpha/lib/cluster.ts +++ b/packages/@aws-cdk/aws-msk-alpha/lib/cluster.ts @@ -46,7 +46,7 @@ export abstract class ClusterBase extends core.Resource implements ICluster { /** Manages connections for the cluster */ public get connections(): ec2.Connections { if (!this._connections) { - throw new Error('An imported Cluster cannot manage its security groups'); + throw new core.ValidationError('An imported Cluster cannot manage its security groups', this); } return this._connections; } @@ -477,26 +477,24 @@ export class Cluster extends ClusterBase { }); if (subnetSelection.subnets.length < 2) { - throw Error(`Cluster requires at least 2 subnets, got ${subnetSelection.subnets.length}`); + throw new core.ValidationError(`Cluster requires at least 2 subnets, got ${subnetSelection.subnets.length}`, this); } if (props.encryptionInTransit?.clientBroker === ClientBrokerEncryption.PLAINTEXT && props.clientAuthentication) { - throw Error('To enable client authentication, you must enabled TLS-encrypted traffic between clients and brokers.'); + throw new core.ValidationError('To enable client authentication, you must enabled TLS-encrypted traffic between clients and brokers.', this); } else if ( props.encryptionInTransit?.clientBroker === ClientBrokerEncryption.TLS_PLAINTEXT && (props.clientAuthentication?.saslProps?.scram || props.clientAuthentication?.saslProps?.iam) ) { - throw Error( - 'To enable SASL/SCRAM or IAM authentication, you must only allow TLS-encrypted traffic between clients and brokers.', - ); + throw new core.ValidationError('To enable SASL/SCRAM or IAM authentication, you must only allow TLS-encrypted traffic between clients and brokers.', this); } const volumeSize = props.ebsStorageInfo?.volumeSize ?? 1000; // Minimum: 1 GiB, maximum: 16384 GiB if (volumeSize < 1 || volumeSize > 16384) { - throw Error('EBS volume size should be in the range 1-16384'); + throw new core.ValidationError('EBS volume size should be in the range 1-16384', this); } const instanceType = props.instanceType @@ -507,12 +505,12 @@ export class Cluster extends ClusterBase { if (props.storageMode && props.storageMode === StorageMode.TIERED) { if (!props.kafkaVersion.isTieredStorageCompatible()) { - throw Error(`To deploy a tiered cluster you must select a compatible Kafka version, got ${props.kafkaVersion.version}`); + throw new core.ValidationError(`To deploy a tiered cluster you must select a compatible Kafka version, got ${props.kafkaVersion.version}`, this); } if (instanceType === this.mskInstanceType( ec2.InstanceType.of(ec2.InstanceClass.T3, ec2.InstanceSize.SMALL), )) { - throw Error('Tiered storage doesn\'t support broker type t3.small'); + throw new core.ValidationError('Tiered storage doesn\'t support broker type t3.small', this); } } @@ -883,9 +881,7 @@ export class Cluster extends ClusterBase { installLatestAwsSdk: false, }); } else { - throw Error( - 'Cannot create users if an authentication KMS key has not been created/provided.', - ); + throw new core.ValidationError('Cannot create users if an authentication KMS key has not been created/provided.', this); } } } diff --git a/packages/@aws-cdk/aws-msk-alpha/lib/serverless-cluster.ts b/packages/@aws-cdk/aws-msk-alpha/lib/serverless-cluster.ts index 883d24b9c0948..6ddf4a02695fc 100644 --- a/packages/@aws-cdk/aws-msk-alpha/lib/serverless-cluster.ts +++ b/packages/@aws-cdk/aws-msk-alpha/lib/serverless-cluster.ts @@ -1,5 +1,5 @@ import * as ec2 from 'aws-cdk-lib/aws-ec2'; -import { Fn, Lazy, Names } from 'aws-cdk-lib'; +import { Fn, Lazy, Names, ValidationError } from 'aws-cdk-lib'; import * as constructs from 'constructs'; import { ClusterBase, ICluster } from '.'; import { CfnServerlessCluster } from 'aws-cdk-lib/aws-msk'; @@ -87,7 +87,7 @@ export class ServerlessCluster extends ClusterBase { addConstructMetadata(this, props); if (props.vpcConfigs.length < 1 || props.vpcConfigs.length > 5) { - throw Error(`\`vpcConfigs\` must contain between 1 and 5 configurations, got ${props.vpcConfigs.length} configurations.`); + throw new ValidationError(`\`vpcConfigs\` must contain between 1 and 5 configurations, got ${props.vpcConfigs.length} configurations.`, this); } const vpcConfigs = props.vpcConfigs.map((vpcConfig, index) => this._renderVpcConfig(vpcConfig, index)); @@ -127,16 +127,14 @@ export class ServerlessCluster extends ClusterBase { const subnetSelection = vpcConfig.vpc.selectSubnets(vpcConfig.vpcSubnets); if (subnetSelection.subnets.length < 2) { - throw Error( - `Cluster requires at least 2 subnets, got ${subnetSelection.subnets.length} subnet.`, - ); + throw new ValidationError(`Cluster requires at least 2 subnets, got ${subnetSelection.subnets.length} subnet.`, this); } let securityGroups: ec2.ISecurityGroup[] = []; if (vpcConfig.securityGroups) { if (vpcConfig.securityGroups.length < 1 || vpcConfig.securityGroups.length > 5) { - throw Error(`\`securityGroups\` must contain between 1 and 5 elements, got ${vpcConfig.securityGroups.length} elements.`); + throw new ValidationError(`\`securityGroups\` must contain between 1 and 5 elements, got ${vpcConfig.securityGroups.length} elements.`, this); } securityGroups = vpcConfig.securityGroups; } else {