Skip to content

Commit 271e439

Browse files
feat(fsx): throw ValidationErrors instead of untyped Errors (#34120)
### Issue # (if applicable) Relates to #32569 ### Reason for this change Untyped Errors are not recommended. ### Description of changes Change Error to ValidationError / UnscopedValidationError ### 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 f2c5f26 commit 271e439

File tree

4 files changed

+34
-29
lines changed

4 files changed

+34
-29
lines changed

Diff for: packages/aws-cdk-lib/.eslintrc.js

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ const enableNoThrowDefaultErrorIn = [
5757
'aws-elasticloadbalancingv2',
5858
'aws-elasticloadbalancingv2-actions',
5959
'aws-elasticloadbalancingv2-targets',
60+
'aws-fsx',
6061
'aws-kinesisfirehose',
6162
'aws-lambda',
6263
'aws-logs',

Diff for: packages/aws-cdk-lib/aws-fsx/lib/daily-automatic-backup-start-time.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { UnscopedValidationError } from '../../core';
2+
13
/**
24
* Properties required for setting up a daily automatic backup time.
35
*/
@@ -59,10 +61,10 @@ export class DailyAutomaticBackupStartTime {
5961
*/
6062
private validate(hour: number, minute: number) {
6163
if (!Number.isInteger(hour) || hour < 0 || hour > 23) {
62-
throw new Error(`dailyAutomaticBackupStartTime hour must be an integer between 0 and 24. received: ${hour}`);
64+
throw new UnscopedValidationError(`dailyAutomaticBackupStartTime hour must be an integer between 0 and 24. received: ${hour}`);
6365
}
6466
if (!Number.isInteger(minute) || minute < 0 || minute > 59) {
65-
throw new Error(`dailyAutomaticBackupStartTime minute must be an integer between 0 and 59. received: ${minute}`);
67+
throw new UnscopedValidationError(`dailyAutomaticBackupStartTime minute must be an integer between 0 and 59. received: ${minute}`);
6668
}
6769
}
6870
}

Diff for: packages/aws-cdk-lib/aws-fsx/lib/lustre-file-system.ts

+25-25
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { FileSystemAttributes, FileSystemBase, FileSystemProps, IFileSystem, Sto
44
import { CfnFileSystem } from './fsx.generated';
55
import { LustreMaintenanceTime } from './maintenance-time';
66
import { Connections, ISecurityGroup, ISubnet, Port, SecurityGroup } from '../../aws-ec2';
7-
import { Aws, Duration, Token } from '../../core';
7+
import { Aws, Duration, Token, ValidationError } from '../../core';
88
import { addConstructMetadata } from '../../core/lib/metadata-resource';
99

1010
/**
@@ -388,7 +388,7 @@ export class LustreFileSystem extends FileSystemBase {
388388
if (!driveCacheType) return;
389389

390390
if (deploymentType !== LustreDeploymentType.PERSISTENT_1 || storageType !== StorageType.HDD) {
391-
throw new Error(`driveCacheType can only be set for PERSISTENT_1 HDD storage type, got: ${deploymentType} and ${storageType}`);
391+
throw new ValidationError(`driveCacheType can only be set for PERSISTENT_1 HDD storage type, got: ${deploymentType} and ${storageType}`, this);
392392
}
393393
}
394394

@@ -399,7 +399,7 @@ export class LustreFileSystem extends FileSystemBase {
399399
if (!storageType) return;
400400

401401
if (storageType === StorageType.HDD && deploymentType !== LustreDeploymentType.PERSISTENT_1) {
402-
throw new Error(`Storage type HDD is only supported for PERSISTENT_1 deployment type, got: ${deploymentType}`);
402+
throw new ValidationError(`Storage type HDD is only supported for PERSISTENT_1 deployment type, got: ${deploymentType}`, this);
403403
}
404404
}
405405

@@ -413,7 +413,7 @@ export class LustreFileSystem extends FileSystemBase {
413413

414414
if (fileSystemTypeVersion === FileSystemTypeVersion.V_2_10) {
415415
if (!deploymentType.startsWith('SCRATCH') && deploymentType !== LustreDeploymentType.PERSISTENT_1) {
416-
throw new Error('fileSystemTypeVersion V_2_10 is only supported for SCRATCH and PERSISTENT_1 deployment types');
416+
throw new ValidationError('fileSystemTypeVersion V_2_10 is only supported for SCRATCH and PERSISTENT_1 deployment types', this);
417417
}
418418
}
419419

@@ -426,10 +426,10 @@ export class LustreFileSystem extends FileSystemBase {
426426
private validateAutoImportPolicy(deploymentType: LustreDeploymentType, importPath?: string, autoImportPolicy?: LustreAutoImportPolicy): void {
427427
if (autoImportPolicy === undefined) { return; }
428428
if (importPath === undefined) {
429-
throw new Error('autoImportPolicy requires importPath to be defined');
429+
throw new ValidationError('autoImportPolicy requires importPath to be defined', this);
430430
}
431431
if (deploymentType === LustreDeploymentType.PERSISTENT_2) {
432-
throw new Error('autoImportPolicy is not supported with PERSISTENT_2 deployments');
432+
throw new ValidationError('autoImportPolicy is not supported with PERSISTENT_2 deployments', this);
433433
}
434434
}
435435

@@ -439,19 +439,19 @@ export class LustreFileSystem extends FileSystemBase {
439439
private validateExportPath(exportPath?: string, importPath?: string): void {
440440
if (exportPath === undefined) { return; }
441441
if (importPath === undefined) {
442-
throw new Error('Cannot define an export path without also defining an import path');
442+
throw new ValidationError('Cannot define an export path without also defining an import path', this);
443443
}
444444

445445
if (Token.isUnresolved(exportPath) && Token.isUnresolved(importPath)) { return; }
446446

447447
if (Token.isUnresolved(importPath) !== Token.isUnresolved(exportPath)) {
448-
throw new Error('The importPath and exportPath must each be Tokens or not Tokens, you cannot use a mix');
448+
throw new ValidationError('The importPath and exportPath must each be Tokens or not Tokens, you cannot use a mix', this);
449449
}
450450
if (!exportPath.startsWith(importPath)) {
451-
throw new Error(`The export path "${exportPath}" is invalid. Expecting the format: s3://{IMPORT_PATH}/optional-prefix`);
451+
throw new ValidationError(`The export path "${exportPath}" is invalid. Expecting the format: s3://{IMPORT_PATH}/optional-prefix`, this);
452452
}
453453
if (exportPath.length > 900) {
454-
throw new Error(`The export path "${exportPath}" exceeds the maximum length of 900 characters`);
454+
throw new ValidationError(`The export path "${exportPath}" exceeds the maximum length of 900 characters`, this);
455455
}
456456
}
457457

@@ -462,7 +462,7 @@ export class LustreFileSystem extends FileSystemBase {
462462
if (importedFileChunkSize === undefined) { return; }
463463

464464
if (importedFileChunkSize < 1 || importedFileChunkSize > 512000) {
465-
throw new Error(`importedFileChunkSize cannot be ${importedFileChunkSize} MiB. It must be a value from 1 to 512,000 MiB`);
465+
throw new ValidationError(`importedFileChunkSize cannot be ${importedFileChunkSize} MiB. It must be a value from 1 to 512,000 MiB`, this);
466466
}
467467
}
468468

@@ -475,10 +475,10 @@ export class LustreFileSystem extends FileSystemBase {
475475
const regexp = /^s3:\/\//;
476476

477477
if (importPath.search(regexp) === -1) {
478-
throw new Error(`The import path "${importPath}" is invalid. Expecting the format: s3://{BUCKET_NAME}/optional-prefix`);
478+
throw new ValidationError(`The import path "${importPath}" is invalid. Expecting the format: s3://{BUCKET_NAME}/optional-prefix`, this);
479479
}
480480
if (importPath.length > 900) {
481-
throw new Error(`The import path "${importPath}" exceeds the maximum length of 900 characters`);
481+
throw new ValidationError(`The import path "${importPath}" exceeds the maximum length of 900 characters`, this);
482482
}
483483
}
484484

@@ -495,21 +495,21 @@ export class LustreFileSystem extends FileSystemBase {
495495
if (perUnitStorageThroughput === undefined) { return; }
496496

497497
if (deploymentType !== LustreDeploymentType.PERSISTENT_1 && deploymentType !== LustreDeploymentType.PERSISTENT_2) {
498-
throw new Error('perUnitStorageThroughput can only be set for the PERSISTENT_1/PERSISTENT_2 deployment types, received: ' + deploymentType);
498+
throw new ValidationError('perUnitStorageThroughput can only be set for the PERSISTENT_1/PERSISTENT_2 deployment types, received: ' + deploymentType, this);
499499
}
500500

501501
if (deploymentType === LustreDeploymentType.PERSISTENT_1) {
502502
if (storageType === StorageType.HDD && ![12, 40].includes(perUnitStorageThroughput)) {
503-
throw new Error(`perUnitStorageThroughput must be 12 or 40 MB/s/TiB for PERSISTENT_1 HDD storage, got: ${perUnitStorageThroughput}`);
503+
throw new ValidationError(`perUnitStorageThroughput must be 12 or 40 MB/s/TiB for PERSISTENT_1 HDD storage, got: ${perUnitStorageThroughput}`, this);
504504
}
505505
if ((storageType === undefined || storageType === StorageType.SSD) && ![50, 100, 200].includes(perUnitStorageThroughput)) {
506-
throw new Error('perUnitStorageThroughput must be 50, 100, or 200 MB/s/TiB for PERSISTENT_1 SSD storage, got: ' + perUnitStorageThroughput);
506+
throw new ValidationError('perUnitStorageThroughput must be 50, 100, or 200 MB/s/TiB for PERSISTENT_1 SSD storage, got: ' + perUnitStorageThroughput, this);
507507
}
508508
}
509509

510510
if (deploymentType === LustreDeploymentType.PERSISTENT_2) {
511511
if (![125, 250, 500, 1000].includes(perUnitStorageThroughput)) {
512-
throw new Error(`perUnitStorageThroughput must be 125, 250, 500 or 1000 MB/s/TiB for PERSISTENT_2 deployment type, got: ${perUnitStorageThroughput}`);
512+
throw new ValidationError(`perUnitStorageThroughput must be 125, 250, 500 or 1000 MB/s/TiB for PERSISTENT_2 deployment type, got: ${perUnitStorageThroughput}`, this);
513513
}
514514
}
515515
}
@@ -527,7 +527,7 @@ export class LustreFileSystem extends FileSystemBase {
527527
): void {
528528
if (deploymentType === LustreDeploymentType.SCRATCH_1) {
529529
if (![1200, 2400, 3600].includes(storageCapacity) && storageCapacity % 3600 !== 0) {
530-
throw new Error(`storageCapacity must be 1,200, 2,400, 3,600, or a multiple of 3,600 for SCRATCH_1 deployment type, got ${storageCapacity}.`);
530+
throw new ValidationError(`storageCapacity must be 1,200, 2,400, 3,600, or a multiple of 3,600 for SCRATCH_1 deployment type, got ${storageCapacity}.`, this);
531531
}
532532
}
533533

@@ -536,21 +536,21 @@ export class LustreFileSystem extends FileSystemBase {
536536
|| deploymentType === LustreDeploymentType.SCRATCH_2
537537
) {
538538
if (![1200, 2400].includes(storageCapacity) && storageCapacity % 2400 !== 0) {
539-
throw new Error(`storageCapacity must be 1,200, 2,400, or a multiple of 2,400 for SCRATCH_2 and PERSISTENT_2 deployment types, got ${storageCapacity}`);
539+
throw new ValidationError(`storageCapacity must be 1,200, 2,400, or a multiple of 2,400 for SCRATCH_2 and PERSISTENT_2 deployment types, got ${storageCapacity}`, this);
540540
}
541541
}
542542

543543
if (deploymentType === LustreDeploymentType.PERSISTENT_1) {
544544
if (storageType === StorageType.HDD) {
545545
if (perUnitStorageThroughput === 12 && storageCapacity % 6000 !== 0) {
546-
throw new Error(`storageCapacity must be a multiple of 6,000 for PERSISTENT_1 HDD storage with 12 MB/s/TiB throughput, got ${storageCapacity}`);
546+
throw new ValidationError(`storageCapacity must be a multiple of 6,000 for PERSISTENT_1 HDD storage with 12 MB/s/TiB throughput, got ${storageCapacity}`, this);
547547
}
548548
if (perUnitStorageThroughput === 40 && storageCapacity % 1800 !== 0) {
549-
throw new Error(`storageCapacity must be a multiple of 1,800 for PERSISTENT_1 HDD storage with 40 MB/s/TiB throughput, got ${storageCapacity}`);
549+
throw new ValidationError(`storageCapacity must be a multiple of 1,800 for PERSISTENT_1 HDD storage with 40 MB/s/TiB throughput, got ${storageCapacity}`, this);
550550
}
551551
} else {
552552
if (![1200, 2400].includes(storageCapacity) && storageCapacity % 2400 !== 0) {
553-
throw new Error(`storageCapacity must be 1,200, 2,400, or a multiple of 2,400 for PERSISTENT_1 SSD storage, got ${storageCapacity}`);
553+
throw new ValidationError(`storageCapacity must be 1,200, 2,400, or a multiple of 2,400 for PERSISTENT_1 SSD storage, got ${storageCapacity}`, this);
554554
}
555555
}
556556
}
@@ -564,11 +564,11 @@ export class LustreFileSystem extends FileSystemBase {
564564
const automaticBackupRetentionDays = automaticBackupRetention.toDays();
565565

566566
if ([LustreDeploymentType.SCRATCH_1, LustreDeploymentType.SCRATCH_2].includes(deploymentType) && automaticBackupRetentionDays > 0) {
567-
throw new Error('automatic backups is not supported on scratch file systems');
567+
throw new ValidationError('automatic backups is not supported on scratch file systems', this);
568568
}
569569

570570
if (automaticBackupRetentionDays > 90) {
571-
throw new Error(`automaticBackupRetention period must be between 0 and 90 days. received: ${automaticBackupRetentionDays}`);
571+
throw new ValidationError(`automaticBackupRetention period must be between 0 and 90 days. received: ${automaticBackupRetentionDays}`, this);
572572
}
573573
}
574574
}
@@ -583,7 +583,7 @@ export class LustreFileSystem extends FileSystemBase {
583583
const automaticBackupDisabled = !automaticBackupRetention || automaticBackupRetention?.toDays() === Duration.days(0).toDays();
584584

585585
if (dailyAutomaticBackupStartTime && automaticBackupDisabled) {
586-
throw new Error('automaticBackupRetention period must be set a non-zero day when dailyAutomaticBackupStartTime is set');
586+
throw new ValidationError('automaticBackupRetention period must be set a non-zero day when dailyAutomaticBackupStartTime is set', this);
587587
}
588588
}
589589
}

Diff for: packages/aws-cdk-lib/aws-fsx/lib/maintenance-time.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { UnscopedValidationError } from '../../core';
2+
13
/**
24
* Enum for representing all the days of the week
35
*/
@@ -97,10 +99,10 @@ export class LustreMaintenanceTime {
9799
*/
98100
private validate(hour: number, minute: number) {
99101
if (!Number.isInteger(hour) || hour < 0 || hour > 23) {
100-
throw new Error('Maintenance time hour must be an integer between 0 and 23');
102+
throw new UnscopedValidationError('Maintenance time hour must be an integer between 0 and 23');
101103
}
102104
if (!Number.isInteger(minute) || minute < 0 || minute > 59) {
103-
throw new Error('Maintenance time minute must be an integer between 0 and 59');
105+
throw new UnscopedValidationError('Maintenance time minute must be an integer between 0 and 59');
104106
}
105107
}
106108
}

0 commit comments

Comments
 (0)