@@ -15,7 +15,7 @@ import * as cloudwatch from '../../aws-cloudwatch';
15
15
import * as iam from '../../aws-iam' ;
16
16
import * as lambda from '../../aws-lambda' ;
17
17
import * as s3 from '../../aws-s3' ;
18
- import { ArnFormat , IResource , Lazy , Resource , Stack , Token , Duration , Names , FeatureFlags , Annotations } from '../../core' ;
18
+ import { ArnFormat , IResource , Lazy , Resource , Stack , Token , Duration , Names , FeatureFlags , Annotations , ValidationError } from '../../core' ;
19
19
import { addConstructMetadata , MethodMetadata } from '../../core/lib/metadata-resource' ;
20
20
import { CLOUDFRONT_DEFAULT_SECURITY_POLICY_TLS_V1_2_2021 } from '../../cx-api' ;
21
21
@@ -332,7 +332,7 @@ export class Distribution extends Resource implements IDistribution {
332
332
if ( props . certificate ) {
333
333
const certificateRegion = Stack . of ( this ) . splitArn ( props . certificate . certificateArn , ArnFormat . SLASH_RESOURCE_NAME ) . region ;
334
334
if ( ! Token . isUnresolved ( certificateRegion ) && certificateRegion !== 'us-east-1' ) {
335
- throw new Error ( `Distribution certificates must be in the us-east-1 region and the certificate you provided is in ${ certificateRegion } .` ) ;
335
+ throw new ValidationError ( `Distribution certificates must be in the us-east-1 region and the certificate you provided is in ${ certificateRegion } .` , this ) ;
336
336
}
337
337
338
338
if ( ( props . domainNames ?? [ ] ) . length === 0 ) {
@@ -491,7 +491,7 @@ export class Distribution extends Resource implements IDistribution {
491
491
@MethodMetadata ( )
492
492
public metricOriginLatency ( props ?: cloudwatch . MetricOptions ) : cloudwatch . Metric {
493
493
if ( this . publishAdditionalMetrics !== true ) {
494
- throw new Error ( 'Origin latency metric is only available if \'publishAdditionalMetrics\' is set \'true\'' ) ;
494
+ throw new ValidationError ( 'Origin latency metric is only available if \'publishAdditionalMetrics\' is set \'true\'' , this ) ;
495
495
}
496
496
return this . metric ( 'OriginLatency' , props ) ;
497
497
}
@@ -508,7 +508,7 @@ export class Distribution extends Resource implements IDistribution {
508
508
@MethodMetadata ( )
509
509
public metricCacheHitRate ( props ?: cloudwatch . MetricOptions ) : cloudwatch . Metric {
510
510
if ( this . publishAdditionalMetrics !== true ) {
511
- throw new Error ( 'Cache hit rate metric is only available if \'publishAdditionalMetrics\' is set \'true\'' ) ;
511
+ throw new ValidationError ( 'Cache hit rate metric is only available if \'publishAdditionalMetrics\' is set \'true\'' , this ) ;
512
512
}
513
513
return this . metric ( 'CacheHitRate' , props ) ;
514
514
}
@@ -523,7 +523,7 @@ export class Distribution extends Resource implements IDistribution {
523
523
@MethodMetadata ( )
524
524
public metric401ErrorRate ( props ?: cloudwatch . MetricOptions ) : cloudwatch . Metric {
525
525
if ( this . publishAdditionalMetrics !== true ) {
526
- throw new Error ( '401 error rate metric is only available if \'publishAdditionalMetrics\' is set \'true\'' ) ;
526
+ throw new ValidationError ( '401 error rate metric is only available if \'publishAdditionalMetrics\' is set \'true\'' , this ) ;
527
527
}
528
528
return this . metric ( '401ErrorRate' , props ) ;
529
529
}
@@ -538,7 +538,7 @@ export class Distribution extends Resource implements IDistribution {
538
538
@MethodMetadata ( )
539
539
public metric403ErrorRate ( props ?: cloudwatch . MetricOptions ) : cloudwatch . Metric {
540
540
if ( this . publishAdditionalMetrics !== true ) {
541
- throw new Error ( '403 error rate metric is only available if \'publishAdditionalMetrics\' is set \'true\'' ) ;
541
+ throw new ValidationError ( '403 error rate metric is only available if \'publishAdditionalMetrics\' is set \'true\'' , this ) ;
542
542
}
543
543
return this . metric ( '403ErrorRate' , props ) ;
544
544
}
@@ -553,7 +553,7 @@ export class Distribution extends Resource implements IDistribution {
553
553
@MethodMetadata ( )
554
554
public metric404ErrorRate ( props ?: cloudwatch . MetricOptions ) : cloudwatch . Metric {
555
555
if ( this . publishAdditionalMetrics !== true ) {
556
- throw new Error ( '404 error rate metric is only available if \'publishAdditionalMetrics\' is set \'true\'' ) ;
556
+ throw new ValidationError ( '404 error rate metric is only available if \'publishAdditionalMetrics\' is set \'true\'' , this ) ;
557
557
}
558
558
return this . metric ( '404ErrorRate' , props ) ;
559
559
}
@@ -568,7 +568,7 @@ export class Distribution extends Resource implements IDistribution {
568
568
@MethodMetadata ( )
569
569
public metric502ErrorRate ( props ?: cloudwatch . MetricOptions ) : cloudwatch . Metric {
570
570
if ( this . publishAdditionalMetrics !== true ) {
571
- throw new Error ( '502 error rate metric is only available if \'publishAdditionalMetrics\' is set \'true\'' ) ;
571
+ throw new ValidationError ( '502 error rate metric is only available if \'publishAdditionalMetrics\' is set \'true\'' , this ) ;
572
572
}
573
573
return this . metric ( '502ErrorRate' , props ) ;
574
574
}
@@ -583,7 +583,7 @@ export class Distribution extends Resource implements IDistribution {
583
583
@MethodMetadata ( )
584
584
public metric503ErrorRate ( props ?: cloudwatch . MetricOptions ) : cloudwatch . Metric {
585
585
if ( this . publishAdditionalMetrics !== true ) {
586
- throw new Error ( '503 error rate metric is only available if \'publishAdditionalMetrics\' is set \'true\'' ) ;
586
+ throw new ValidationError ( '503 error rate metric is only available if \'publishAdditionalMetrics\' is set \'true\'' , this ) ;
587
587
}
588
588
return this . metric ( '503ErrorRate' , props ) ;
589
589
}
@@ -598,7 +598,7 @@ export class Distribution extends Resource implements IDistribution {
598
598
@MethodMetadata ( )
599
599
public metric504ErrorRate ( props ?: cloudwatch . MetricOptions ) : cloudwatch . Metric {
600
600
if ( this . publishAdditionalMetrics !== true ) {
601
- throw new Error ( '504 error rate metric is only available if \'publishAdditionalMetrics\' is set \'true\'' ) ;
601
+ throw new ValidationError ( '504 error rate metric is only available if \'publishAdditionalMetrics\' is set \'true\'' , this ) ;
602
602
}
603
603
return this . metric ( '504ErrorRate' , props ) ;
604
604
}
@@ -613,7 +613,7 @@ export class Distribution extends Resource implements IDistribution {
613
613
@MethodMetadata ( )
614
614
public addBehavior ( pathPattern : string , origin : IOrigin , behaviorOptions : AddBehaviorOptions = { } ) {
615
615
if ( pathPattern === '*' ) {
616
- throw new Error ( 'Only the default behavior can have a path pattern of \'*\'' ) ;
616
+ throw new ValidationError ( 'Only the default behavior can have a path pattern of \'*\'' , this ) ;
617
617
}
618
618
const originId = this . addOrigin ( origin ) ;
619
619
this . additionalBehaviors . push ( new CacheBehavior ( originId , { pathPattern, ...behaviorOptions } ) ) ;
@@ -651,7 +651,7 @@ export class Distribution extends Resource implements IDistribution {
651
651
@MethodMetadata ( )
652
652
public attachWebAclId ( webAclId : string ) {
653
653
if ( this . webAclId ) {
654
- throw new Error ( 'A WebACL has already been attached to this distribution' ) ;
654
+ throw new ValidationError ( 'A WebACL has already been attached to this distribution' , this ) ;
655
655
}
656
656
this . validateWebAclId ( webAclId ) ;
657
657
this . webAclId = webAclId ;
@@ -661,7 +661,7 @@ export class Distribution extends Resource implements IDistribution {
661
661
if ( webAclId . startsWith ( 'arn:' ) ) {
662
662
const webAclRegion = Stack . of ( this ) . splitArn ( webAclId , ArnFormat . SLASH_RESOURCE_NAME ) . region ;
663
663
if ( ! Token . isUnresolved ( webAclRegion ) && webAclRegion !== 'us-east-1' ) {
664
- throw new Error ( `WebACL for CloudFront distributions must be created in the us-east-1 region; received ${ webAclRegion } ` ) ;
664
+ throw new ValidationError ( `WebACL for CloudFront distributions must be created in the us-east-1 region; received ${ webAclRegion } ` , this ) ;
665
665
}
666
666
}
667
667
}
@@ -681,13 +681,13 @@ export class Distribution extends Resource implements IDistribution {
681
681
const originId = originBindConfig . originProperty ?. id ?? generatedId ;
682
682
const duplicateId = this . boundOrigins . find ( boundOrigin => boundOrigin . originProperty ?. id === originBindConfig . originProperty ?. id ) ;
683
683
if ( duplicateId ) {
684
- throw new Error ( `Origin with id ${ duplicateId . originProperty ?. id } already exists. OriginIds must be unique within a distribution` ) ;
684
+ throw new ValidationError ( `Origin with id ${ duplicateId . originProperty ?. id } already exists. OriginIds must be unique within a distribution` , this ) ;
685
685
}
686
686
if ( ! originBindConfig . failoverConfig ) {
687
687
this . boundOrigins . push ( { origin, originId, distributionId, ...originBindConfig } ) ;
688
688
} else {
689
689
if ( isFailoverOrigin ) {
690
- throw new Error ( 'An Origin cannot use an Origin with its own failover configuration as its fallback origin!' ) ;
690
+ throw new ValidationError ( 'An Origin cannot use an Origin with its own failover configuration as its fallback origin!' , this ) ;
691
691
}
692
692
const groupIndex = this . originGroups . length + 1 ;
693
693
const originGroupId = Names . uniqueId ( new Construct ( this , `OriginGroup${ groupIndex } ` ) ) . slice ( - ORIGIN_ID_MAX_LENGTH ) ;
@@ -716,7 +716,7 @@ export class Distribution extends Resource implements IDistribution {
716
716
) : void {
717
717
statusCodes = statusCodes ?? [ 500 , 502 , 503 , 504 ] ;
718
718
if ( statusCodes . length === 0 ) {
719
- throw new Error ( 'fallbackStatusCodes cannot be empty' ) ;
719
+ throw new ValidationError ( 'fallbackStatusCodes cannot be empty' , this ) ;
720
720
}
721
721
this . originGroups . push ( {
722
722
failoverCriteria : {
@@ -766,7 +766,7 @@ export class Distribution extends Resource implements IDistribution {
766
766
767
767
return this . errorResponses . map ( errorConfig => {
768
768
if ( ! errorConfig . responseHttpStatus && ! errorConfig . ttl && ! errorConfig . responsePagePath ) {
769
- throw new Error ( 'A custom error response without either a \'responseHttpStatus\', \'ttl\' or \'responsePagePath\' is not valid.' ) ;
769
+ throw new ValidationError ( 'A custom error response without either a \'responseHttpStatus\', \'ttl\' or \'responsePagePath\' is not valid.' , this ) ;
770
770
}
771
771
772
772
return {
@@ -783,7 +783,7 @@ export class Distribution extends Resource implements IDistribution {
783
783
private renderLogging ( props : DistributionProps ) : CfnDistribution . LoggingProperty | undefined {
784
784
if ( ! props . enableLogging && ! props . logBucket ) { return undefined ; }
785
785
if ( props . enableLogging === false && props . logBucket ) {
786
- throw new Error ( 'Explicitly disabled logging but provided a logging bucket.' ) ;
786
+ throw new ValidationError ( 'Explicitly disabled logging but provided a logging bucket.' , this ) ;
787
787
}
788
788
789
789
const bucket = props . logBucket ?? new s3 . Bucket ( this , 'LoggingBucket' , {
0 commit comments