Skip to content

Commit f2581a3

Browse files
committed
feat(sns-subscriptions): throw typed errors
1 parent 5432367 commit f2581a3

File tree

4 files changed

+9
-10
lines changed

4 files changed

+9
-10
lines changed

packages/aws-cdk-lib/.eslintrc.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ const noThrowDefaultErrorNotYetSupported = [
3535
'aws-secretsmanager',
3636
'aws-servicecatalog',
3737
'aws-servicediscovery',
38-
'aws-sns-subscriptions',
3938
'aws-stepfunctions',
4039
'aws-stepfunctions-tasks',
4140
'core',

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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 3 additions & 3 deletions
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.
@@ -44,11 +44,11 @@ export class UrlSubscription implements sns.ITopicSubscription {
4444
constructor(private readonly url: string, private readonly props: UrlSubscriptionProps = {}) {
4545
this.unresolvedUrl = Token.isUnresolved(url);
4646
if (!this.unresolvedUrl && !url.startsWith('http://') && !url.startsWith('https://')) {
47-
throw new Error('URL must start with either http:// or https://');
47+
throw new UnscopedValidationError('URL must start with either http:// or https://');
4848
}
4949

5050
if (this.unresolvedUrl && props.protocol === undefined) {
51-
throw new Error('Must provide protocol if url is unresolved');
51+
throw new UnscopedValidationError('Must provide protocol if url is unresolved');
5252
}
5353

5454
if (this.unresolvedUrl) {

0 commit comments

Comments
 (0)