Skip to content

Commit 6bf8095

Browse files
authored
feat(efs): throw ValidationErrors instead of untyped Errors (#33885)
### Issue # (if applicable) Relates to #32569 ### Reason for this change Untyped Errors are not recommended. ### Description of changes Change `Error` to `ValidationError`. ### Describe any new or updated permissions being added None ### Description of how you validated changes Existing tests. Exemptions granted as this is a refactor of existing code. ### 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*
1 parent 2dc5d70 commit 6bf8095

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

packages/aws-cdk-lib/.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const enableNoThrowDefaultErrorIn = [
4848
'aws-cognito',
4949
'aws-docdb',
5050
'aws-ecr',
51+
'aws-efs',
5152
'aws-elasticloadbalancing',
5253
'aws-elasticloadbalancingv2',
5354
'aws-elasticloadbalancingv2-actions',

packages/aws-cdk-lib/aws-efs/lib/access-point.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Construct } from 'constructs';
22
import { IFileSystem } from './efs-file-system';
33
import { CfnAccessPoint } from './efs.generated';
4-
import { ArnFormat, IResource, Resource, Stack, Tags, Token } from '../../core';
4+
import { ArnFormat, IResource, Resource, Stack, Tags, Token, ValidationError } from '../../core';
55
import { addConstructMetadata } from '../../core/lib/metadata-resource';
66

77
/**
@@ -215,7 +215,7 @@ export class AccessPoint extends AccessPointBase {
215215

216216
const clientToken = props.clientToken;
217217
if ((clientToken?.length === 0 || (clientToken && clientToken.length > 64)) && !Token.isUnresolved(clientToken)) {
218-
throw new Error(`The length of \'clientToken\' must range from 1 to 64 characters, got: ${clientToken.length} characters`);
218+
throw new ValidationError(`The length of \'clientToken\' must range from 1 to 64 characters, got: ${clientToken.length} characters`, this);
219219
}
220220

221221
const resource = new CfnAccessPoint(this, 'Resource', {
@@ -260,20 +260,20 @@ class ImportedAccessPoint extends AccessPointBase {
260260

261261
if (!attrs.accessPointId) {
262262
if (!attrs.accessPointArn) {
263-
throw new Error('One of accessPointId or AccessPointArn is required!');
263+
throw new ValidationError('One of accessPointId or AccessPointArn is required!', this);
264264
}
265265

266266
this.accessPointArn = attrs.accessPointArn;
267267
let maybeApId = Stack.of(scope).splitArn(attrs.accessPointArn, ArnFormat.SLASH_RESOURCE_NAME).resourceName;
268268

269269
if (!maybeApId) {
270-
throw new Error('ARN for AccessPoint must provide the resource name.');
270+
throw new ValidationError('ARN for AccessPoint must provide the resource name.', this);
271271
}
272272

273273
this.accessPointId = maybeApId;
274274
} else {
275275
if (attrs.accessPointArn) {
276-
throw new Error('Only one of accessPointId or AccessPointArn can be provided!');
276+
throw new ValidationError('Only one of accessPointId or AccessPointArn can be provided!', this);
277277
}
278278

279279
this.accessPointId = attrs.accessPointId;
@@ -289,7 +289,7 @@ class ImportedAccessPoint extends AccessPointBase {
289289

290290
public get fileSystem() {
291291
if (!this._fileSystem) {
292-
throw new Error("fileSystem is only available if 'fromAccessPointAttributes()' is used and a fileSystem is passed in as an attribute.");
292+
throw new ValidationError("fileSystem is only available if 'fromAccessPointAttributes()' is used and a fileSystem is passed in as an attribute.", this);
293293
}
294294

295295
return this._fileSystem;

packages/aws-cdk-lib/aws-efs/lib/efs-file-system.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { CfnFileSystem, CfnMountTarget } from './efs.generated';
44
import * as ec2 from '../../aws-ec2';
55
import * as iam from '../../aws-iam';
66
import * as kms from '../../aws-kms';
7-
import { ArnFormat, FeatureFlags, Lazy, RemovalPolicy, Resource, Size, Stack, Tags, Token } from '../../core';
7+
import { ArnFormat, FeatureFlags, Lazy, RemovalPolicy, Resource, Size, Stack, Tags, Token, ValidationError } from '../../core';
88
import { addConstructMetadata, MethodMetadata } from '../../core/lib/metadata-resource';
99
import * as cxapi from '../../cx-api';
1010

@@ -735,21 +735,21 @@ export class FileSystem extends FileSystemBase {
735735
this.props = props;
736736

737737
if (props.performanceMode === PerformanceMode.MAX_IO && props.oneZone) {
738-
throw new Error('performanceMode MAX_IO is not supported for One Zone file systems.');
738+
throw new ValidationError('performanceMode MAX_IO is not supported for One Zone file systems.', this);
739739
}
740740

741741
if (props.oneZone) { this.oneZoneValidation(); }
742742

743743
if (props.throughputMode === ThroughputMode.PROVISIONED && props.provisionedThroughputPerSecond === undefined) {
744-
throw new Error('Property provisionedThroughputPerSecond is required when throughputMode is PROVISIONED');
744+
throw new ValidationError('Property provisionedThroughputPerSecond is required when throughputMode is PROVISIONED', this);
745745
}
746746

747747
if (props.throughputMode === ThroughputMode.ELASTIC && props.performanceMode === PerformanceMode.MAX_IO) {
748-
throw new Error('ThroughputMode ELASTIC is not supported for file systems with performanceMode MAX_IO');
748+
throw new ValidationError('ThroughputMode ELASTIC is not supported for file systems with performanceMode MAX_IO', this);
749749
}
750750

751751
if (props.replicationConfiguration && props.replicationOverwriteProtection === ReplicationOverwriteProtection.DISABLED) {
752-
throw new Error('Cannot configure \'replicationConfiguration\' when \'replicationOverwriteProtection\' is set to \'DISABLED\'');
752+
throw new ValidationError('Cannot configure \'replicationConfiguration\' when \'replicationOverwriteProtection\' is set to \'DISABLED\'', this);
753753
}
754754

755755
// we explicitly use 'undefined' to represent 'false' to maintain backwards compatibility since
@@ -891,13 +891,13 @@ export class FileSystem extends FileSystemBase {
891891
private oneZoneValidation() {
892892
// validate when props.oneZone is enabled
893893
if (this.props.vpcSubnets && !this.props.vpcSubnets.availabilityZones) {
894-
throw new Error('When oneZone is enabled and vpcSubnets defined, vpcSubnets.availabilityZones can not be undefined.');
894+
throw new ValidationError('When oneZone is enabled and vpcSubnets defined, vpcSubnets.availabilityZones can not be undefined.', this);
895895
}
896896
// when vpcSubnets.availabilityZones is defined
897897
if (this.props.vpcSubnets && this.props.vpcSubnets.availabilityZones) {
898898
// it has to be only one az
899899
if (this.props.vpcSubnets.availabilityZones?.length !== 1) {
900-
throw new Error('When oneZone is enabled, vpcSubnets.availabilityZones should exactly have one zone.');
900+
throw new ValidationError('When oneZone is enabled, vpcSubnets.availabilityZones should exactly have one zone.', this);
901901
}
902902
// it has to be in availabilityZones
903903
// but we only check this when vpc.availabilityZones are valid(not dummy values nore unresolved tokens)
@@ -906,7 +906,7 @@ export class FileSystem extends FileSystemBase {
906906
if (this.props.vpc.availabilityZones.every(isNotUnresolvedToken) &&
907907
this.props.vpc.availabilityZones.every(isNotDummy) &&
908908
!this.props.vpc.availabilityZones.includes(this.props.vpcSubnets.availabilityZones[0])) {
909-
throw new Error('vpcSubnets.availabilityZones specified is not in vpc.availabilityZones.');
909+
throw new ValidationError('vpcSubnets.availabilityZones specified is not in vpc.availabilityZones.', this);
910910
}
911911
}
912912
}
@@ -950,7 +950,7 @@ class ImportedFileSystem extends FileSystemBase {
950950
addConstructMetadata(this, attrs);
951951

952952
if (!!attrs.fileSystemId === !!attrs.fileSystemArn) {
953-
throw new Error('One of fileSystemId or fileSystemArn, but not both, must be provided.');
953+
throw new ValidationError('One of fileSystemId or fileSystemArn, but not both, must be provided.', this);
954954
}
955955

956956
this.fileSystemArn = attrs.fileSystemArn ?? Stack.of(scope).formatArn({
@@ -962,7 +962,7 @@ class ImportedFileSystem extends FileSystemBase {
962962
const parsedArn = Stack.of(scope).splitArn(this.fileSystemArn, ArnFormat.SLASH_RESOURCE_NAME);
963963

964964
if (!parsedArn.resourceName) {
965-
throw new Error(`Invalid FileSystem Arn ${this.fileSystemArn}`);
965+
throw new ValidationError(`Invalid FileSystem Arn ${this.fileSystemArn}`, this);
966966
}
967967

968968
this.fileSystemId = attrs.fileSystemId ?? parsedArn.resourceName;

0 commit comments

Comments
 (0)