Skip to content

Commit 02cb5a4

Browse files
feat(msk): throw ValidationError instead of untyped errors (#34214)
### Issue # (if applicable) `aws-msk-alpha` for #32569 ### Description of changes ValidationErrors everywhere <!-- What code changes did you make? Have you made any important design decisions? What AWS use cases does this change enable? To enable the use cases, which AWS service features are utilized? --> ### Describe any new or updated permissions being added n/a <!-- What new or updated IAM permissions are needed to support the changes being introduced ? --> ### Description of how you validated changes Existing tests. Exemptions granted as this is basically a refactor of existing code. <!--Have you added any unit tests and/or integration tests?--> ### 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* Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 58c2631 commit 02cb5a4

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

packages/@aws-cdk/aws-msk-alpha/.eslintrc.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,12 @@ baseConfig.parserOptions.project = __dirname + '/tsconfig.json';
44
baseConfig.rules['import/no-extraneous-dependencies'] = ['error', { devDependencies: true, peerDependencies: true } ];
55
baseConfig.rules['import/order'] = 'off';
66
baseConfig.rules['@aws-cdk/invalid-cfn-imports'] = 'off';
7+
baseConfig.rules['@cdklabs/no-throw-default-error'] = ['error'];
8+
baseConfig.overrides.push({
9+
files: ["./test/**"],
10+
rules: {
11+
"@cdklabs/no-throw-default-error": "off",
12+
},
13+
});
714

815
module.exports = baseConfig;

packages/@aws-cdk/aws-msk-alpha/lib/cluster.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export abstract class ClusterBase extends core.Resource implements ICluster {
4646
/** Manages connections for the cluster */
4747
public get connections(): ec2.Connections {
4848
if (!this._connections) {
49-
throw new Error('An imported Cluster cannot manage its security groups');
49+
throw new core.ValidationError('An imported Cluster cannot manage its security groups', this);
5050
}
5151
return this._connections;
5252
}
@@ -477,26 +477,24 @@ export class Cluster extends ClusterBase {
477477
});
478478

479479
if (subnetSelection.subnets.length < 2) {
480-
throw Error(`Cluster requires at least 2 subnets, got ${subnetSelection.subnets.length}`);
480+
throw new core.ValidationError(`Cluster requires at least 2 subnets, got ${subnetSelection.subnets.length}`, this);
481481
}
482482

483483
if (props.encryptionInTransit?.clientBroker === ClientBrokerEncryption.PLAINTEXT && props.clientAuthentication) {
484-
throw Error('To enable client authentication, you must enabled TLS-encrypted traffic between clients and brokers.');
484+
throw new core.ValidationError('To enable client authentication, you must enabled TLS-encrypted traffic between clients and brokers.', this);
485485
} else if (
486486
props.encryptionInTransit?.clientBroker ===
487487
ClientBrokerEncryption.TLS_PLAINTEXT &&
488488
(props.clientAuthentication?.saslProps?.scram ||
489489
props.clientAuthentication?.saslProps?.iam)
490490
) {
491-
throw Error(
492-
'To enable SASL/SCRAM or IAM authentication, you must only allow TLS-encrypted traffic between clients and brokers.',
493-
);
491+
throw new core.ValidationError('To enable SASL/SCRAM or IAM authentication, you must only allow TLS-encrypted traffic between clients and brokers.', this);
494492
}
495493

496494
const volumeSize = props.ebsStorageInfo?.volumeSize ?? 1000;
497495
// Minimum: 1 GiB, maximum: 16384 GiB
498496
if (volumeSize < 1 || volumeSize > 16384) {
499-
throw Error('EBS volume size should be in the range 1-16384');
497+
throw new core.ValidationError('EBS volume size should be in the range 1-16384', this);
500498
}
501499

502500
const instanceType = props.instanceType
@@ -507,12 +505,12 @@ export class Cluster extends ClusterBase {
507505

508506
if (props.storageMode && props.storageMode === StorageMode.TIERED) {
509507
if (!props.kafkaVersion.isTieredStorageCompatible()) {
510-
throw Error(`To deploy a tiered cluster you must select a compatible Kafka version, got ${props.kafkaVersion.version}`);
508+
throw new core.ValidationError(`To deploy a tiered cluster you must select a compatible Kafka version, got ${props.kafkaVersion.version}`, this);
511509
}
512510
if (instanceType === this.mskInstanceType(
513511
ec2.InstanceType.of(ec2.InstanceClass.T3, ec2.InstanceSize.SMALL),
514512
)) {
515-
throw Error('Tiered storage doesn\'t support broker type t3.small');
513+
throw new core.ValidationError('Tiered storage doesn\'t support broker type t3.small', this);
516514
}
517515
}
518516

@@ -883,9 +881,7 @@ export class Cluster extends ClusterBase {
883881
installLatestAwsSdk: false,
884882
});
885883
} else {
886-
throw Error(
887-
'Cannot create users if an authentication KMS key has not been created/provided.',
888-
);
884+
throw new core.ValidationError('Cannot create users if an authentication KMS key has not been created/provided.', this);
889885
}
890886
}
891887
}

packages/@aws-cdk/aws-msk-alpha/lib/serverless-cluster.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as ec2 from 'aws-cdk-lib/aws-ec2';
2-
import { Fn, Lazy, Names } from 'aws-cdk-lib';
2+
import { Fn, Lazy, Names, ValidationError } from 'aws-cdk-lib';
33
import * as constructs from 'constructs';
44
import { ClusterBase, ICluster } from '.';
55
import { CfnServerlessCluster } from 'aws-cdk-lib/aws-msk';
@@ -87,7 +87,7 @@ export class ServerlessCluster extends ClusterBase {
8787
addConstructMetadata(this, props);
8888

8989
if (props.vpcConfigs.length < 1 || props.vpcConfigs.length > 5) {
90-
throw Error(`\`vpcConfigs\` must contain between 1 and 5 configurations, got ${props.vpcConfigs.length} configurations.`);
90+
throw new ValidationError(`\`vpcConfigs\` must contain between 1 and 5 configurations, got ${props.vpcConfigs.length} configurations.`, this);
9191
}
9292

9393
const vpcConfigs = props.vpcConfigs.map((vpcConfig, index) => this._renderVpcConfig(vpcConfig, index));
@@ -127,16 +127,14 @@ export class ServerlessCluster extends ClusterBase {
127127
const subnetSelection = vpcConfig.vpc.selectSubnets(vpcConfig.vpcSubnets);
128128

129129
if (subnetSelection.subnets.length < 2) {
130-
throw Error(
131-
`Cluster requires at least 2 subnets, got ${subnetSelection.subnets.length} subnet.`,
132-
);
130+
throw new ValidationError(`Cluster requires at least 2 subnets, got ${subnetSelection.subnets.length} subnet.`, this);
133131
}
134132

135133
let securityGroups: ec2.ISecurityGroup[] = [];
136134

137135
if (vpcConfig.securityGroups) {
138136
if (vpcConfig.securityGroups.length < 1 || vpcConfig.securityGroups.length > 5) {
139-
throw Error(`\`securityGroups\` must contain between 1 and 5 elements, got ${vpcConfig.securityGroups.length} elements.`);
137+
throw new ValidationError(`\`securityGroups\` must contain between 1 and 5 elements, got ${vpcConfig.securityGroups.length} elements.`, this);
140138
}
141139
securityGroups = vpcConfig.securityGroups;
142140
} else {

0 commit comments

Comments
 (0)