@@ -18,6 +18,7 @@ import {
18
18
ResourceProps ,
19
19
Stack ,
20
20
Token ,
21
+ ValidationError ,
21
22
} from '../../core' ;
22
23
import { addConstructMetadata , MethodMetadata } from '../../core/lib/metadata-resource' ;
23
24
import * as cxapi from '../../cx-api' ;
@@ -163,7 +164,7 @@ abstract class KeyBase extends Resource implements IKey {
163
164
164
165
if ( ! this . policy ) {
165
166
if ( allowNoOp ) { return { statementAdded : false } ; }
166
- throw new Error ( `Unable to add statement to IAM resource policy for KMS key: ${ JSON . stringify ( stack . resolve ( this . keyArn ) ) } ` ) ;
167
+ throw new ValidationError ( `Unable to add statement to IAM resource policy for KMS key: ${ JSON . stringify ( stack . resolve ( this . keyArn ) ) } ` , this ) ;
167
168
}
168
169
169
170
this . policy . addStatements ( statement ) ;
@@ -630,7 +631,7 @@ export class Key extends KeyBase {
630
631
631
632
const keyResourceName = Stack . of ( scope ) . splitArn ( keyArn , ArnFormat . SLASH_RESOURCE_NAME ) . resourceName ;
632
633
if ( ! keyResourceName ) {
633
- throw new Error ( `KMS key ARN must be in the format 'arn:<partition>:kms:<region>:<account>:key/<keyId>', got: '${ keyArn } '` ) ;
634
+ throw new ValidationError ( `KMS key ARN must be in the format 'arn:<partition>:kms:<region>:<account>:key/<keyId>', got: '${ keyArn } '` , scope ) ;
634
635
}
635
636
636
637
return new Import ( keyResourceName , {
@@ -671,9 +672,9 @@ export class Key extends KeyBase {
671
672
// throw an exception suggesting to use the other importing methods instead.
672
673
// We might make this parsing logic smarter later,
673
674
// but let's start by erroring out.
674
- throw new Error ( 'Could not parse the PolicyDocument of the passed AWS::KMS::Key resource because it contains CloudFormation functions. ' +
675
+ throw new ValidationError ( 'Could not parse the PolicyDocument of the passed AWS::KMS::Key resource because it contains CloudFormation functions. ' +
675
676
'This makes it impossible to create a mutable IKey from that Policy. ' +
676
- 'You have to use fromKeyArn instead, passing it the ARN attribute property of the low-level CfnKey' ) ;
677
+ 'You have to use fromKeyArn instead, passing it the ARN attribute property of the low-level CfnKey' , cfnKey ) ;
677
678
}
678
679
679
680
// change the key policy of the L1, so that all changes done in the L2 are reflected in the resulting template
@@ -730,7 +731,7 @@ export class Key extends KeyBase {
730
731
}
731
732
}
732
733
if ( Token . isUnresolved ( options . aliasName ) ) {
733
- throw new Error ( 'All arguments to Key.fromLookup() must be concrete (no Tokens)' ) ;
734
+ throw new ValidationError ( 'All arguments to Key.fromLookup() must be concrete (no Tokens)' , scope ) ;
734
735
}
735
736
736
737
const attributes : cxapi . KeyContextResponse = ContextProvider . getValue ( scope , {
@@ -814,25 +815,25 @@ export class Key extends KeyBase {
814
815
const keySpec = props . keySpec ?? KeySpec . SYMMETRIC_DEFAULT ;
815
816
const keyUsage = props . keyUsage ?? KeyUsage . ENCRYPT_DECRYPT ;
816
817
if ( denyLists [ keyUsage ] . includes ( keySpec ) ) {
817
- throw new Error ( `key spec '${ keySpec } ' is not valid with usage '${ keyUsage } '` ) ;
818
+ throw new ValidationError ( `key spec '${ keySpec } ' is not valid with usage '${ keyUsage } '` , this ) ;
818
819
}
819
820
820
821
if ( keySpec . startsWith ( 'HMAC' ) && props . enableKeyRotation ) {
821
- throw new Error ( 'key rotation cannot be enabled on HMAC keys' ) ;
822
+ throw new ValidationError ( 'key rotation cannot be enabled on HMAC keys' , this ) ;
822
823
}
823
824
824
825
if ( keySpec !== KeySpec . SYMMETRIC_DEFAULT && props . enableKeyRotation ) {
825
- throw new Error ( 'key rotation cannot be enabled on asymmetric keys' ) ;
826
+ throw new ValidationError ( 'key rotation cannot be enabled on asymmetric keys' , this ) ;
826
827
}
827
828
828
829
this . enableKeyRotation = props . enableKeyRotation ;
829
830
830
831
if ( props . rotationPeriod ) {
831
832
if ( props . enableKeyRotation === false ) {
832
- throw new Error ( '\'rotationPeriod\' cannot be specified when \'enableKeyRotation\' is disabled' ) ;
833
+ throw new ValidationError ( '\'rotationPeriod\' cannot be specified when \'enableKeyRotation\' is disabled' , this ) ;
833
834
}
834
835
if ( props . rotationPeriod . toDays ( ) < 90 || props . rotationPeriod . toDays ( ) > 2560 ) {
835
- throw new Error ( `'rotationPeriod' value must between 90 and 2650 days. Received: ${ props . rotationPeriod . toDays ( ) } ` ) ;
836
+ throw new ValidationError ( `'rotationPeriod' value must between 90 and 2650 days. Received: ${ props . rotationPeriod . toDays ( ) } ` , this ) ;
836
837
}
837
838
// If rotationPeriod is specified, enableKeyRotation is set to true by default
838
839
if ( props . enableKeyRotation === undefined ) {
@@ -845,7 +846,7 @@ export class Key extends KeyBase {
845
846
this . policy = props . policy ?? new iam . PolicyDocument ( ) ;
846
847
if ( defaultKeyPoliciesFeatureEnabled ) {
847
848
if ( props . trustAccountIdentities === false ) {
848
- throw new Error ( '`trustAccountIdentities` cannot be false if the @aws-cdk/aws-kms:defaultKeyPolicies feature flag is set' ) ;
849
+ throw new ValidationError ( '`trustAccountIdentities` cannot be false if the @aws-cdk/aws-kms:defaultKeyPolicies feature flag is set' , this ) ;
849
850
}
850
851
851
852
this . trustAccountIdentities = true ;
@@ -866,7 +867,7 @@ export class Key extends KeyBase {
866
867
if ( props . pendingWindow ) {
867
868
pendingWindowInDays = props . pendingWindow . toDays ( ) ;
868
869
if ( pendingWindowInDays < 7 || pendingWindowInDays > 30 ) {
869
- throw new Error ( `'pendingWindow' value must between 7 and 30 days. Received: ${ pendingWindowInDays } ` ) ;
870
+ throw new ValidationError ( `'pendingWindow' value must between 7 and 30 days. Received: ${ pendingWindowInDays } ` , this ) ;
870
871
}
871
872
}
872
873
0 commit comments