@@ -6,6 +6,7 @@ import { ITopic } from './topic-base';
6
6
import { PolicyStatement , ServicePrincipal } from '../../aws-iam' ;
7
7
import { IQueue } from '../../aws-sqs' ;
8
8
import { Resource } from '../../core' ;
9
+ import { ValidationError } from '../../core/lib/errors' ;
9
10
10
11
/**
11
12
* Options for creating a new subscription
@@ -114,12 +115,12 @@ export class Subscription extends Resource {
114
115
SubscriptionProtocol . FIREHOSE ,
115
116
]
116
117
. indexOf ( props . protocol ) < 0 ) {
117
- throw new Error ( 'Raw message delivery can only be enabled for HTTP, HTTPS, SQS, and Firehose subscriptions.' ) ;
118
+ throw new ValidationError ( 'Raw message delivery can only be enabled for HTTP, HTTPS, SQS, and Firehose subscriptions.' , this ) ;
118
119
}
119
120
120
121
if ( props . filterPolicy ) {
121
122
if ( Object . keys ( props . filterPolicy ) . length > 5 ) {
122
- throw new Error ( 'A filter policy can have a maximum of 5 attribute names.' ) ;
123
+ throw new ValidationError ( 'A filter policy can have a maximum of 5 attribute names.' , this ) ;
123
124
}
124
125
125
126
this . filterPolicy = Object . entries ( props . filterPolicy )
@@ -131,22 +132,22 @@ export class Subscription extends Resource {
131
132
let total = 1 ;
132
133
Object . values ( this . filterPolicy ) . forEach ( filter => { total *= filter . length ; } ) ;
133
134
if ( total > 150 ) {
134
- throw new Error ( `The total combination of values (${ total } ) must not exceed 150.` ) ;
135
+ throw new ValidationError ( `The total combination of values (${ total } ) must not exceed 150.` , this ) ;
135
136
}
136
137
} else if ( props . filterPolicyWithMessageBody ) {
137
138
if ( Object . keys ( props . filterPolicyWithMessageBody ) . length > 5 ) {
138
- throw new Error ( 'A filter policy can have a maximum of 5 attribute names.' ) ;
139
+ throw new ValidationError ( 'A filter policy can have a maximum of 5 attribute names.' , this ) ;
139
140
}
140
141
this . filterPolicyWithMessageBody = props . filterPolicyWithMessageBody ;
141
142
}
142
143
143
144
if ( props . protocol === SubscriptionProtocol . FIREHOSE && ! props . subscriptionRoleArn ) {
144
- throw new Error ( 'Subscription role arn is required field for subscriptions with a firehose protocol.' ) ;
145
+ throw new ValidationError ( 'Subscription role arn is required field for subscriptions with a firehose protocol.' , this ) ;
145
146
}
146
147
147
148
// Format filter policy
148
149
const filterPolicy = this . filterPolicyWithMessageBody
149
- ? buildFilterPolicyWithMessageBody ( this . filterPolicyWithMessageBody )
150
+ ? buildFilterPolicyWithMessageBody ( this , this . filterPolicyWithMessageBody )
150
151
: this . filterPolicy ;
151
152
152
153
this . deadLetterQueue = this . buildDeadLetterQueue ( props ) ;
@@ -167,7 +168,7 @@ export class Subscription extends Resource {
167
168
168
169
private renderDeliveryPolicy ( deliveryPolicy : DeliveryPolicy , protocol : SubscriptionProtocol ) : any {
169
170
if ( ! [ SubscriptionProtocol . HTTP , SubscriptionProtocol . HTTPS ] . includes ( protocol ) ) {
170
- throw new Error ( `Delivery policy is only supported for HTTP and HTTPS subscriptions, got: ${ protocol } ` ) ;
171
+ throw new ValidationError ( `Delivery policy is only supported for HTTP and HTTPS subscriptions, got: ${ protocol } ` , this ) ;
171
172
}
172
173
const { healthyRetryPolicy, throttlePolicy } = deliveryPolicy ;
173
174
if ( healthyRetryPolicy ) {
@@ -176,45 +177,45 @@ export class Subscription extends Resource {
176
177
const maxDelayTarget = healthyRetryPolicy . maxDelayTarget ;
177
178
if ( minDelayTarget !== undefined ) {
178
179
if ( minDelayTarget . toMilliseconds ( ) % 1000 !== 0 ) {
179
- throw new Error ( `minDelayTarget must be a whole number of seconds, got: ${ minDelayTarget } ` ) ;
180
+ throw new ValidationError ( `minDelayTarget must be a whole number of seconds, got: ${ minDelayTarget } ` , this ) ;
180
181
}
181
182
const minDelayTargetSecs = minDelayTarget . toSeconds ( ) ;
182
183
if ( minDelayTargetSecs < 1 || minDelayTargetSecs > delayTargetLimitSecs ) {
183
- throw new Error ( `minDelayTarget must be between 1 and ${ delayTargetLimitSecs } seconds inclusive, got: ${ minDelayTargetSecs } s` ) ;
184
+ throw new ValidationError ( `minDelayTarget must be between 1 and ${ delayTargetLimitSecs } seconds inclusive, got: ${ minDelayTargetSecs } s` , this ) ;
184
185
}
185
186
}
186
187
if ( maxDelayTarget !== undefined ) {
187
188
if ( maxDelayTarget . toMilliseconds ( ) % 1000 !== 0 ) {
188
- throw new Error ( `maxDelayTarget must be a whole number of seconds, got: ${ maxDelayTarget } ` ) ;
189
+ throw new ValidationError ( `maxDelayTarget must be a whole number of seconds, got: ${ maxDelayTarget } ` , this ) ;
189
190
}
190
191
const maxDelayTargetSecs = maxDelayTarget . toSeconds ( ) ;
191
192
if ( maxDelayTargetSecs < 1 || maxDelayTargetSecs > delayTargetLimitSecs ) {
192
- throw new Error ( `maxDelayTarget must be between 1 and ${ delayTargetLimitSecs } seconds inclusive, got: ${ maxDelayTargetSecs } s` ) ;
193
+ throw new ValidationError ( `maxDelayTarget must be between 1 and ${ delayTargetLimitSecs } seconds inclusive, got: ${ maxDelayTargetSecs } s` , this ) ;
193
194
}
194
195
if ( ( minDelayTarget !== undefined ) && minDelayTarget . toSeconds ( ) > maxDelayTargetSecs ) {
195
- throw new Error ( 'minDelayTarget must not exceed maxDelayTarget' ) ;
196
+ throw new ValidationError ( 'minDelayTarget must not exceed maxDelayTarget' , this ) ;
196
197
}
197
198
}
198
199
199
200
const numRetriesLimit = 100 ;
200
201
if ( healthyRetryPolicy . numRetries && ( healthyRetryPolicy . numRetries < 0 || healthyRetryPolicy . numRetries > numRetriesLimit ) ) {
201
- throw new Error ( `numRetries must be between 0 and ${ numRetriesLimit } inclusive, got: ${ healthyRetryPolicy . numRetries } ` ) ;
202
+ throw new ValidationError ( `numRetries must be between 0 and ${ numRetriesLimit } inclusive, got: ${ healthyRetryPolicy . numRetries } ` , this ) ;
202
203
}
203
204
const { numNoDelayRetries, numMinDelayRetries, numMaxDelayRetries } = healthyRetryPolicy ;
204
205
if ( numNoDelayRetries && ( numNoDelayRetries < 0 || ! Number . isInteger ( numNoDelayRetries ) ) ) {
205
- throw new Error ( `numNoDelayRetries must be an integer zero or greater, got: ${ numNoDelayRetries } ` ) ;
206
+ throw new ValidationError ( `numNoDelayRetries must be an integer zero or greater, got: ${ numNoDelayRetries } ` , this ) ;
206
207
}
207
208
if ( numMinDelayRetries && ( numMinDelayRetries < 0 || ! Number . isInteger ( numMinDelayRetries ) ) ) {
208
- throw new Error ( `numMinDelayRetries must be an integer zero or greater, got: ${ numMinDelayRetries } ` ) ;
209
+ throw new ValidationError ( `numMinDelayRetries must be an integer zero or greater, got: ${ numMinDelayRetries } ` , this ) ;
209
210
}
210
211
if ( numMaxDelayRetries && ( numMaxDelayRetries < 0 || ! Number . isInteger ( numMaxDelayRetries ) ) ) {
211
- throw new Error ( `numMaxDelayRetries must be an integer zero or greater, got: ${ numMaxDelayRetries } ` ) ;
212
+ throw new ValidationError ( `numMaxDelayRetries must be an integer zero or greater, got: ${ numMaxDelayRetries } ` , this ) ;
212
213
}
213
214
}
214
215
if ( throttlePolicy ) {
215
216
const maxReceivesPerSecond = throttlePolicy . maxReceivesPerSecond ;
216
217
if ( maxReceivesPerSecond !== undefined && ( maxReceivesPerSecond < 1 || ! Number . isInteger ( maxReceivesPerSecond ) ) ) {
217
- throw new Error ( `maxReceivesPerSecond must be an integer greater than zero, got: ${ maxReceivesPerSecond } ` ) ;
218
+ throw new ValidationError ( `maxReceivesPerSecond must be an integer greater than zero, got: ${ maxReceivesPerSecond } ` , this ) ;
218
219
}
219
220
}
220
221
return {
@@ -320,6 +321,7 @@ export enum SubscriptionProtocol {
320
321
}
321
322
322
323
function buildFilterPolicyWithMessageBody (
324
+ scope : Construct ,
323
325
inputObject : { [ key : string ] : FilterOrPolicy } ,
324
326
depth = 1 ,
325
327
totalCombinationValues = [ 1 ] ,
@@ -328,7 +330,7 @@ function buildFilterPolicyWithMessageBody(
328
330
329
331
for ( const [ key , filterOrPolicy ] of Object . entries ( inputObject ) ) {
330
332
if ( filterOrPolicy . isPolicy ( ) ) {
331
- result [ key ] = buildFilterPolicyWithMessageBody ( filterOrPolicy . policyDoc , depth + 1 , totalCombinationValues ) ;
333
+ result [ key ] = buildFilterPolicyWithMessageBody ( scope , filterOrPolicy . policyDoc , depth + 1 , totalCombinationValues ) ;
332
334
} else if ( filterOrPolicy . isFilter ( ) ) {
333
335
const filter = filterOrPolicy . filterDoc . conditions ;
334
336
result [ key ] = filter ;
@@ -338,7 +340,7 @@ function buildFilterPolicyWithMessageBody(
338
340
339
341
// https://docs.aws.amazon.com/sns/latest/dg/subscription-filter-policy-constraints.html
340
342
if ( totalCombinationValues [ 0 ] > 150 ) {
341
- throw new Error ( `The total combination of values (${ totalCombinationValues } ) must not exceed 150.` ) ;
343
+ throw new ValidationError ( `The total combination of values (${ totalCombinationValues } ) must not exceed 150.` , scope ) ;
342
344
}
343
345
344
346
return result ;
0 commit comments