Skip to content

Commit 98e352d

Browse files
authored
feat(sns-subscriptions): throw ValidationErrors instead of untyped errors (#34436)
### Issue Relates to #32569 ### Reason for this change untyped Errors are not recommended ### Description of changes `ValidationError`s everywhere ### Describe any new or updated permissions being added None ### Description of how you validated changes Existing tests. Exemptions granted as this is a refactor of existing code. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 0f71ee1 commit 98e352d

File tree

4 files changed

+9
-10
lines changed

4 files changed

+9
-10
lines changed

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

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ const noThrowDefaultErrorNotYetSupported = [
3030
'aws-scheduler',
3131
'aws-secretsmanager',
3232
'aws-servicecatalog',
33-
'aws-sns-subscriptions',
3433
'core',
3534
'custom-resources',
3635
'region-info',

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)