Skip to content

Commit e8d013a

Browse files
committed
feat(sns-subscriptions): throw typed errors
1 parent 0f71ee1 commit e8d013a

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

packages/aws-cdk-lib/aws-sns-subscriptions/lib/lambda.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { SubscriptionProps } from './subscription';
33
import * as iam from '../../aws-iam';
44
import * as lambda from '../../aws-lambda';
55
import * as sns from '../../aws-sns';
6-
import { ArnFormat, Names, Stack, Token } from '../../core';
6+
import { ArnFormat, Names, Stack, Token, ValidationError } from '../../core';
77

88
/**
99
* Properties for a Lambda subscription
@@ -25,7 +25,7 @@ export class LambdaSubscription implements sns.ITopicSubscription {
2525
// Create subscription under *consuming* construct to make sure it ends up
2626
// in the correct stack in cases of cross-stack subscriptions.
2727
if (!Construct.isConstruct(this.fn)) {
28-
throw new Error('The supplied lambda Function object must be an instance of Construct');
28+
throw new ValidationError('The supplied lambda Function object must be an instance of Construct', topic);
2929
}
3030

3131
this.fn.addPermission(`AllowInvoke:${Names.nodeUniqueId(topic.node)}`, {

packages/aws-cdk-lib/aws-sns-subscriptions/lib/sqs.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { SubscriptionProps } from './subscription';
33
import * as iam from '../../aws-iam';
44
import * as sns from '../../aws-sns';
55
import * as sqs from '../../aws-sqs';
6-
import { ArnFormat, FeatureFlags, Names, Stack, Token } from '../../core';
6+
import { ArnFormat, FeatureFlags, Names, Stack, Token, ValidationError } from '../../core';
77
import * as cxapi from '../../cx-api';
88

99
/**
@@ -34,20 +34,20 @@ export class SqsSubscription implements sns.ITopicSubscription {
3434
// Create subscription under *consuming* construct to make sure it ends up
3535
// in the correct stack in cases of cross-stack subscriptions.
3636
if (!Construct.isConstruct(this.queue)) {
37-
throw new Error('The supplied Queue object must be an instance of Construct');
37+
throw new ValidationError('The supplied Queue object must be an instance of Construct', topic);
3838
}
3939
const snsServicePrincipal = new iam.ServicePrincipal('sns.amazonaws.com');
4040

4141
// if the queue is encrypted by AWS managed KMS key (alias/aws/sqs),
4242
// throw error message
4343
if (this.queue.encryptionType === sqs.QueueEncryption.KMS_MANAGED) {
44-
throw new Error('SQS queue encrypted by AWS managed KMS key cannot be used as SNS subscription');
44+
throw new ValidationError('SQS queue encrypted by AWS managed KMS key cannot be used as SNS subscription', topic);
4545
}
4646

4747
// if the dead-letter queue is encrypted by AWS managed KMS key (alias/aws/sqs),
4848
// throw error message
4949
if (this.props.deadLetterQueue && this.props.deadLetterQueue.encryptionType === sqs.QueueEncryption.KMS_MANAGED) {
50-
throw new Error('SQS queue encrypted by AWS managed KMS key cannot be used as dead-letter queue');
50+
throw new ValidationError('SQS queue encrypted by AWS managed KMS key cannot be used as dead-letter queue', topic);
5151
}
5252

5353
// add a statement to the queue resource policy which allows this topic

packages/aws-cdk-lib/aws-sns-subscriptions/lib/url.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { SubscriptionProps } from './subscription';
22
import * as sns from '../../aws-sns';
3-
import { Token } from '../../core';
3+
import { Token, UnscopedValidationError } from '../../core';
44

55
/**
66
* Options for URL subscriptions.
@@ -49,11 +49,11 @@ export class UrlSubscription implements sns.ITopicSubscription {
4949
constructor(private readonly url: string, private readonly props: UrlSubscriptionProps = {}) {
5050
this.unresolvedUrl = Token.isUnresolved(url);
5151
if (!this.unresolvedUrl && !url.startsWith('http://') && !url.startsWith('https://')) {
52-
throw new Error('URL must start with either http:// or https://');
52+
throw new UnscopedValidationError('URL must start with either http:// or https://');
5353
}
5454

5555
if (this.unresolvedUrl && props.protocol === undefined) {
56-
throw new Error('Must provide protocol if url is unresolved');
56+
throw new UnscopedValidationError('Must provide protocol if url is unresolved');
5757
}
5858

5959
if (this.unresolvedUrl) {

0 commit comments

Comments
 (0)