@@ -9,6 +9,7 @@ import { ITrustStore } from './trust-store';
9
9
import * as ec2 from '../../../aws-ec2' ;
10
10
import * as cxschema from '../../../cloud-assembly-schema' ;
11
11
import { Duration , FeatureFlags , Lazy , Resource , Token } from '../../../core' ;
12
+ import { ValidationError } from '../../../core/lib/errors' ;
12
13
import * as cxapi from '../../../cx-api' ;
13
14
import { BaseListener , BaseListenerLookupOptions , IListener } from '../shared/base-listener' ;
14
15
import { HealthCheck } from '../shared/base-target-group' ;
@@ -197,7 +198,7 @@ export class ApplicationListener extends BaseListener implements IApplicationLis
197
198
*/
198
199
public static fromLookup ( scope : Construct , id : string , options : ApplicationListenerLookupOptions ) : IApplicationListener {
199
200
if ( Token . isUnresolved ( options . listenerArn ) ) {
200
- throw new Error ( 'All arguments to look up a load balancer listener must be concrete (no Tokens)' ) ;
201
+ throw new ValidationError ( 'All arguments to look up a load balancer listener must be concrete (no Tokens)' , scope ) ;
201
202
}
202
203
203
204
let listenerProtocol : cxschema . LoadBalancerListenerProtocol | undefined ;
@@ -251,10 +252,10 @@ export class ApplicationListener extends BaseListener implements IApplicationLis
251
252
constructor ( scope : Construct , id : string , props : ApplicationListenerProps ) {
252
253
const [ protocol , port ] = determineProtocolAndPort ( props . protocol , props . port ) ;
253
254
if ( protocol === undefined || port === undefined ) {
254
- throw new Error ( 'At least one of \'port\' or \'protocol\' is required' ) ;
255
+ throw new ValidationError ( 'At least one of \'port\' or \'protocol\' is required' , scope ) ;
255
256
}
256
257
257
- validateMutualAuthentication ( props . mutualAuthentication ) ;
258
+ validateMutualAuthentication ( scope , props . mutualAuthentication ) ;
258
259
259
260
super ( scope , id , {
260
261
loadBalancerArn : props . loadBalancer . loadBalancerArn ,
@@ -290,7 +291,7 @@ export class ApplicationListener extends BaseListener implements IApplicationLis
290
291
} ) ;
291
292
292
293
if ( props . defaultAction && props . defaultTargetGroups ) {
293
- throw new Error ( 'Specify at most one of \'defaultAction\' and \'defaultTargetGroups\'' ) ;
294
+ throw new ValidationError ( 'Specify at most one of \'defaultAction\' and \'defaultTargetGroups\'' , this ) ;
294
295
}
295
296
296
297
if ( props . defaultAction ) {
@@ -361,7 +362,7 @@ export class ApplicationListener extends BaseListener implements IApplicationLis
361
362
* default Action).
362
363
*/
363
364
public addAction ( id : string , props : AddApplicationActionProps ) : void {
364
- checkAddRuleProps ( props ) ;
365
+ checkAddRuleProps ( this , props ) ;
365
366
366
367
if ( props . priority !== undefined ) {
367
368
// New rule
@@ -389,7 +390,7 @@ export class ApplicationListener extends BaseListener implements IApplicationLis
389
390
* become the default Action for this listener).
390
391
*/
391
392
public addTargetGroups ( id : string , props : AddApplicationTargetGroupsProps ) : void {
392
- checkAddRuleProps ( props ) ;
393
+ checkAddRuleProps ( this , props ) ;
393
394
394
395
if ( props . priority !== undefined ) {
395
396
// New rule
@@ -423,7 +424,7 @@ export class ApplicationListener extends BaseListener implements IApplicationLis
423
424
public addTargets ( id : string , props : AddApplicationTargetsProps ) : ApplicationTargetGroup {
424
425
if ( ! this . loadBalancer . vpc ) {
425
426
// eslint-disable-next-line max-len
426
- throw new Error ( 'Can only call addTargets() when using a constructed Load Balancer or an imported Load Balancer with specified vpc; construct a new TargetGroup and use addTargetGroup' ) ;
427
+ throw new ValidationError ( 'Can only call addTargets() when using a constructed Load Balancer or an imported Load Balancer with specified vpc; construct a new TargetGroup and use addTargetGroup' , this ) ;
427
428
}
428
429
429
430
const group = new ApplicationTargetGroup ( this , id + 'Group' , {
@@ -445,7 +446,7 @@ export class ApplicationListener extends BaseListener implements IApplicationLis
445
446
* @deprecated Use `addAction()` instead
446
447
*/
447
448
public addFixedResponse ( id : string , props : AddFixedResponseProps ) {
448
- checkAddRuleProps ( props ) ;
449
+ checkAddRuleProps ( this , props ) ;
449
450
450
451
const fixedResponse : FixedResponse = {
451
452
statusCode : props . statusCode ,
@@ -459,11 +460,11 @@ export class ApplicationListener extends BaseListener implements IApplicationLis
459
460
* Inlining the duplication functionality in v2 only (for now).
460
461
*/
461
462
if ( fixedResponse . statusCode && ! / ^ ( 2 | 4 | 5 ) \d \d $ / . test ( fixedResponse . statusCode ) ) {
462
- throw new Error ( '`statusCode` must be 2XX, 4XX or 5XX.' ) ;
463
+ throw new ValidationError ( '`statusCode` must be 2XX, 4XX or 5XX.' , this ) ;
463
464
}
464
465
465
466
if ( fixedResponse . messageBody && fixedResponse . messageBody . length > 1024 ) {
466
- throw new Error ( '`messageBody` cannot have more than 1024 characters.' ) ;
467
+ throw new ValidationError ( '`messageBody` cannot have more than 1024 characters.' , this ) ;
467
468
}
468
469
469
470
if ( props . priority ) {
@@ -487,7 +488,7 @@ export class ApplicationListener extends BaseListener implements IApplicationLis
487
488
* @deprecated Use `addAction()` instead
488
489
*/
489
490
public addRedirectResponse ( id : string , props : AddRedirectResponseProps ) {
490
- checkAddRuleProps ( props ) ;
491
+ checkAddRuleProps ( this , props ) ;
491
492
const redirectResponse = {
492
493
host : props . host ,
493
494
path : props . path ,
@@ -503,11 +504,11 @@ export class ApplicationListener extends BaseListener implements IApplicationLis
503
504
* Inlining the duplication functionality in v2 only (for now).
504
505
*/
505
506
if ( redirectResponse . protocol && ! / ^ ( H T T P S ? | # \{ p r o t o c o l \} ) $ / i. test ( redirectResponse . protocol ) ) {
506
- throw new Error ( '`protocol` must be HTTP, HTTPS, or #{protocol}.' ) ;
507
+ throw new ValidationError ( '`protocol` must be HTTP, HTTPS, or #{protocol}.' , this ) ;
507
508
}
508
509
509
510
if ( ! redirectResponse . statusCode || ! / ^ H T T P _ 3 0 [ 1 2 ] $ / . test ( redirectResponse . statusCode ) ) {
510
- throw new Error ( '`statusCode` must be HTTP_301 or HTTP_302.' ) ;
511
+ throw new ValidationError ( '`statusCode` must be HTTP_301 or HTTP_302.' , this ) ;
511
512
}
512
513
513
514
if ( props . priority ) {
@@ -698,7 +699,7 @@ abstract class ExternalApplicationListener extends Resource implements IApplicat
698
699
* At least one TargetGroup must be added without conditions.
699
700
*/
700
701
public addTargetGroups ( id : string , props : AddApplicationTargetGroupsProps ) : void {
701
- checkAddRuleProps ( props ) ;
702
+ checkAddRuleProps ( this , props ) ;
702
703
703
704
if ( props . priority !== undefined ) {
704
705
// New rule
@@ -708,7 +709,7 @@ abstract class ExternalApplicationListener extends Resource implements IApplicat
708
709
...props ,
709
710
} ) ;
710
711
} else {
711
- throw new Error ( 'Cannot add default Target Groups to imported ApplicationListener' ) ;
712
+ throw new ValidationError ( 'Cannot add default Target Groups to imported ApplicationListener' , this ) ;
712
713
}
713
714
}
714
715
@@ -725,7 +726,7 @@ abstract class ExternalApplicationListener extends Resource implements IApplicat
725
726
*/
726
727
public addTargets ( _id : string , _props : AddApplicationTargetsProps ) : ApplicationTargetGroup {
727
728
// eslint-disable-next-line max-len
728
- throw new Error ( 'Can only call addTargets() when using a constructed ApplicationListener; construct a new TargetGroup and use addTargetGroup.' ) ;
729
+ throw new ValidationError ( 'Can only call addTargets() when using a constructed ApplicationListener; construct a new TargetGroup and use addTargetGroup.' , this ) ;
729
730
}
730
731
731
732
/**
@@ -747,7 +748,7 @@ abstract class ExternalApplicationListener extends Resource implements IApplicat
747
748
* property here to avoid having CloudFormation attempt to replace your resource.
748
749
*/
749
750
public addAction ( id : string , props : AddApplicationActionProps ) : void {
750
- checkAddRuleProps ( props ) ;
751
+ checkAddRuleProps ( this , props ) ;
751
752
752
753
if ( props . priority !== undefined ) {
753
754
const ruleId = props . removeSuffix ? id : id + 'Rule' ;
@@ -760,7 +761,7 @@ abstract class ExternalApplicationListener extends Resource implements IApplicat
760
761
...props ,
761
762
} ) ;
762
763
} else {
763
- throw new Error ( 'priority must be set for actions added to an imported listener' ) ;
764
+ throw new ValidationError ( 'priority must be set for actions added to an imported listener' , this ) ;
764
765
}
765
766
}
766
767
}
@@ -1036,17 +1037,17 @@ export interface AddFixedResponseProps extends AddRuleProps, FixedResponse {
1036
1037
export interface AddRedirectResponseProps extends AddRuleProps , RedirectResponse {
1037
1038
}
1038
1039
1039
- function checkAddRuleProps ( props : AddRuleProps ) {
1040
+ function checkAddRuleProps ( scope : Construct , props : AddRuleProps ) {
1040
1041
const conditionsCount = props . conditions ?. length || 0 ;
1041
1042
const hasAnyConditions = conditionsCount !== 0 ||
1042
1043
props . hostHeader !== undefined || props . pathPattern !== undefined || props . pathPatterns !== undefined ;
1043
1044
const hasPriority = props . priority !== undefined ;
1044
1045
if ( hasAnyConditions !== hasPriority ) {
1045
- throw new Error ( 'Setting \'conditions\', \'pathPattern\' or \'hostHeader\' also requires \'priority\', and vice versa' ) ;
1046
+ throw new ValidationError ( 'Setting \'conditions\', \'pathPattern\' or \'hostHeader\' also requires \'priority\', and vice versa' , scope ) ;
1046
1047
}
1047
1048
}
1048
1049
1049
- function validateMutualAuthentication ( mutualAuthentication ?: MutualAuthentication ) : void {
1050
+ function validateMutualAuthentication ( scope : Construct , mutualAuthentication ?: MutualAuthentication ) : void {
1050
1051
if ( ! mutualAuthentication ) {
1051
1052
return ;
1052
1053
}
@@ -1055,17 +1056,17 @@ function validateMutualAuthentication(mutualAuthentication?: MutualAuthenticatio
1055
1056
1056
1057
if ( currentMode === MutualAuthenticationMode . VERIFY ) {
1057
1058
if ( ! mutualAuthentication . trustStore ) {
1058
- throw new Error ( `You must set 'trustStore' when 'mode' is '${ MutualAuthenticationMode . VERIFY } '` ) ;
1059
+ throw new ValidationError ( `You must set 'trustStore' when 'mode' is '${ MutualAuthenticationMode . VERIFY } '` , scope ) ;
1059
1060
}
1060
1061
}
1061
1062
1062
1063
if ( currentMode === MutualAuthenticationMode . OFF || currentMode === MutualAuthenticationMode . PASS_THROUGH ) {
1063
1064
if ( mutualAuthentication . trustStore ) {
1064
- throw new Error ( `You cannot set 'trustStore' when 'mode' is '${ MutualAuthenticationMode . OFF } ' or '${ MutualAuthenticationMode . PASS_THROUGH } '` ) ;
1065
+ throw new ValidationError ( `You cannot set 'trustStore' when 'mode' is '${ MutualAuthenticationMode . OFF } ' or '${ MutualAuthenticationMode . PASS_THROUGH } '` , scope ) ;
1065
1066
}
1066
1067
1067
1068
if ( mutualAuthentication . ignoreClientCertificateExpiry !== undefined ) {
1068
- throw new Error ( `You cannot set 'ignoreClientCertificateExpiry' when 'mode' is '${ MutualAuthenticationMode . OFF } ' or '${ MutualAuthenticationMode . PASS_THROUGH } '` ) ;
1069
+ throw new ValidationError ( `You cannot set 'ignoreClientCertificateExpiry' when 'mode' is '${ MutualAuthenticationMode . OFF } ' or '${ MutualAuthenticationMode . PASS_THROUGH } '` , scope ) ;
1069
1070
}
1070
1071
}
1071
1072
}
0 commit comments