@@ -11,7 +11,7 @@ import * as kms from '../../aws-kms';
11
11
import * as logs from '../../aws-logs' ;
12
12
import { CaCertificate } from '../../aws-rds' ;
13
13
import * as secretsmanager from '../../aws-secretsmanager' ;
14
- import { CfnResource , Duration , RemovalPolicy , Resource , Token } from '../../core' ;
14
+ import { CfnResource , Duration , RemovalPolicy , Resource , Token , UnscopedValidationError , ValidationError } from '../../core' ;
15
15
import { addConstructMetadata , MethodMetadata } from '../../core/lib/metadata-resource' ;
16
16
17
17
const MIN_ENGINE_VERSION_FOR_IO_OPTIMIZED_STORAGE = 5 ;
@@ -355,35 +355,35 @@ export class DatabaseCluster extends DatabaseClusterBase {
355
355
356
356
public get instanceIdentifiers ( ) : string [ ] {
357
357
if ( ! this . _instanceIdentifiers ) {
358
- throw new Error ( 'Cannot access `instanceIdentifiers` of an imported cluster without provided instanceIdentifiers' ) ;
358
+ throw new UnscopedValidationError ( 'Cannot access `instanceIdentifiers` of an imported cluster without provided instanceIdentifiers' ) ;
359
359
}
360
360
return this . _instanceIdentifiers ;
361
361
}
362
362
363
363
public get clusterEndpoint ( ) : Endpoint {
364
364
if ( ! this . _clusterEndpoint ) {
365
- throw new Error ( 'Cannot access `clusterEndpoint` of an imported cluster without an endpoint address and port' ) ;
365
+ throw new UnscopedValidationError ( 'Cannot access `clusterEndpoint` of an imported cluster without an endpoint address and port' ) ;
366
366
}
367
367
return this . _clusterEndpoint ;
368
368
}
369
369
370
370
public get clusterReadEndpoint ( ) : Endpoint {
371
371
if ( ! this . _clusterReadEndpoint ) {
372
- throw new Error ( 'Cannot access `clusterReadEndpoint` of an imported cluster without a readerEndpointAddress and port' ) ;
372
+ throw new UnscopedValidationError ( 'Cannot access `clusterReadEndpoint` of an imported cluster without a readerEndpointAddress and port' ) ;
373
373
}
374
374
return this . _clusterReadEndpoint ;
375
375
}
376
376
377
377
public get instanceEndpoints ( ) : Endpoint [ ] {
378
378
if ( ! this . _instanceEndpoints ) {
379
- throw new Error ( 'Cannot access `instanceEndpoints` of an imported cluster without instanceEndpointAddresses and port' ) ;
379
+ throw new UnscopedValidationError ( 'Cannot access `instanceEndpoints` of an imported cluster without instanceEndpointAddresses and port' ) ;
380
380
}
381
381
return this . _instanceEndpoints ;
382
382
}
383
383
384
384
public get securityGroupId ( ) : string {
385
385
if ( ! this . _securityGroupId ) {
386
- throw new Error ( 'Cannot access `securityGroupId` of an imported cluster without securityGroupId' ) ;
386
+ throw new UnscopedValidationError ( 'Cannot access `securityGroupId` of an imported cluster without securityGroupId' ) ;
387
387
}
388
388
return this . _securityGroupId ;
389
389
}
@@ -479,7 +479,7 @@ export class DatabaseCluster extends DatabaseClusterBase {
479
479
// We cannot test whether the subnets are in different AZs, but at least we can test the amount.
480
480
// See https://docs.aws.amazon.com/documentdb/latest/developerguide/replication.html#replication.high-availability
481
481
if ( subnetIds . length < 2 ) {
482
- throw new Error ( `Cluster requires at least 2 subnets, got ${ subnetIds . length } ` ) ;
482
+ throw new ValidationError ( `Cluster requires at least 2 subnets, got ${ subnetIds . length } ` , this ) ;
483
483
}
484
484
485
485
const subnetGroup = new CfnDBSubnetGroup ( this , 'Subnets' , {
@@ -497,7 +497,7 @@ export class DatabaseCluster extends DatabaseClusterBase {
497
497
vpc : this . vpc ,
498
498
} ) ;
499
499
// HACK: Use an escape-hatch to apply a consistent removal policy to the
500
- // security group so we don't get errors when trying to delete the stack.
500
+ // security group so we don't get ValidationErrors when trying to delete the stack.
501
501
const securityGroupRemovalPolicy = this . getSecurityGroupRemovalPolicy ( props ) ;
502
502
( securityGroup . node . defaultChild as CfnResource ) . applyRemovalPolicy ( securityGroupRemovalPolicy , {
503
503
applyToUpdateReplacePolicy : true ,
@@ -529,20 +529,20 @@ export class DatabaseCluster extends DatabaseClusterBase {
529
529
const storageEncrypted = props . storageEncrypted ?? true ;
530
530
531
531
if ( props . kmsKey && ! storageEncrypted ) {
532
- throw new Error ( 'KMS key supplied but storageEncrypted is false' ) ;
532
+ throw new ValidationError ( 'KMS key supplied but storageEncrypted is false' , this ) ;
533
533
}
534
534
535
535
const validEngineVersionRegex = / ^ ( 0 | [ 1 - 9 ] [ 0 - 9 ] * ) \. ( 0 | [ 1 - 9 ] [ 0 - 9 ] * ) \. ( 0 | [ 1 - 9 ] [ 0 - 9 ] * ) $ / ;
536
536
if ( props . engineVersion !== undefined && ! validEngineVersionRegex . test ( props . engineVersion ) ) {
537
- throw new Error ( `Invalid engine version: '${ props . engineVersion } '. Engine version must be in the format x.y.z` ) ;
537
+ throw new ValidationError ( `Invalid engine version: '${ props . engineVersion } '. Engine version must be in the format x.y.z` , this ) ;
538
538
}
539
539
540
540
if (
541
541
props . storageType === StorageType . IOPT1
542
542
&& props . engineVersion !== undefined
543
543
&& Number ( props . engineVersion . split ( '.' ) [ 0 ] ) < MIN_ENGINE_VERSION_FOR_IO_OPTIMIZED_STORAGE
544
544
) {
545
- throw new Error ( `I/O-optimized storage is supported starting with engine version 5.0.0, got '${ props . engineVersion } '` ) ;
545
+ throw new ValidationError ( `I/O-optimized storage is supported starting with engine version 5.0.0, got '${ props . engineVersion } '` , this ) ;
546
546
}
547
547
548
548
// Create the DocDB cluster
@@ -594,7 +594,7 @@ export class DatabaseCluster extends DatabaseClusterBase {
594
594
// Create the instances
595
595
const instanceCount = props . instances ?? DatabaseCluster . DEFAULT_NUM_INSTANCES ;
596
596
if ( instanceCount < 1 ) {
597
- throw new Error ( 'At least one instance is required' ) ;
597
+ throw new ValidationError ( 'At least one instance is required' , this ) ;
598
598
}
599
599
600
600
const instanceRemovalPolicy = this . getInstanceRemovalPolicy ( props ) ;
@@ -651,7 +651,7 @@ export class DatabaseCluster extends DatabaseClusterBase {
651
651
652
652
private getInstanceRemovalPolicy ( props : DatabaseClusterProps ) {
653
653
if ( props . instanceRemovalPolicy === RemovalPolicy . SNAPSHOT ) {
654
- throw new Error ( 'AWS::DocDB::DBInstance does not support the SNAPSHOT removal policy' ) ;
654
+ throw new ValidationError ( 'AWS::DocDB::DBInstance does not support the SNAPSHOT removal policy' , this ) ;
655
655
}
656
656
if ( props . instanceRemovalPolicy ) return props . instanceRemovalPolicy ;
657
657
return ! props . removalPolicy || props . removalPolicy !== RemovalPolicy . SNAPSHOT ?
@@ -660,7 +660,7 @@ export class DatabaseCluster extends DatabaseClusterBase {
660
660
661
661
private getSecurityGroupRemovalPolicy ( props : DatabaseClusterProps ) {
662
662
if ( props . securityGroupRemovalPolicy === RemovalPolicy . SNAPSHOT ) {
663
- throw new Error ( 'AWS::EC2::SecurityGroup does not support the SNAPSHOT removal policy' ) ;
663
+ throw new ValidationError ( 'AWS::EC2::SecurityGroup does not support the SNAPSHOT removal policy' , this ) ;
664
664
}
665
665
if ( props . securityGroupRemovalPolicy ) return props . securityGroupRemovalPolicy ;
666
666
return ! props . removalPolicy || props . removalPolicy !== RemovalPolicy . SNAPSHOT ?
@@ -676,13 +676,13 @@ export class DatabaseCluster extends DatabaseClusterBase {
676
676
@MethodMetadata ( )
677
677
public addRotationSingleUser ( automaticallyAfter ?: Duration ) : secretsmanager . SecretRotation {
678
678
if ( ! this . secret ) {
679
- throw new Error ( 'Cannot add single user rotation for a cluster without secret.' ) ;
679
+ throw new ValidationError ( 'Cannot add single user rotation for a cluster without secret.' , this ) ;
680
680
}
681
681
682
682
const id = 'RotationSingleUser' ;
683
683
const existing = this . node . tryFindChild ( id ) ;
684
684
if ( existing ) {
685
- throw new Error ( 'A single user rotation was already added to this cluster.' ) ;
685
+ throw new ValidationError ( 'A single user rotation was already added to this cluster.' , this ) ;
686
686
}
687
687
688
688
return new secretsmanager . SecretRotation ( this , id , {
@@ -702,7 +702,7 @@ export class DatabaseCluster extends DatabaseClusterBase {
702
702
@MethodMetadata ( )
703
703
public addRotationMultiUser ( id : string , options : RotationMultiUserOptions ) : secretsmanager . SecretRotation {
704
704
if ( ! this . secret ) {
705
- throw new Error ( 'Cannot add multi user rotation for a cluster without secret.' ) ;
705
+ throw new ValidationError ( 'Cannot add multi user rotation for a cluster without secret.' , this ) ;
706
706
}
707
707
return new secretsmanager . SecretRotation ( this , id , {
708
708
secret : options . secret ,
0 commit comments