Skip to content

feat(sns-subscriptions): throw ValidationErrors instead of untyped errors #34436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/aws-cdk-lib/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const noThrowDefaultErrorNotYetSupported = [
'aws-scheduler',
'aws-secretsmanager',
'aws-servicecatalog',
'aws-sns-subscriptions',
'core',
'custom-resources',
'region-info',
Expand Down
4 changes: 2 additions & 2 deletions packages/aws-cdk-lib/aws-sns-subscriptions/lib/lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { SubscriptionProps } from './subscription';
import * as iam from '../../aws-iam';
import * as lambda from '../../aws-lambda';
import * as sns from '../../aws-sns';
import { ArnFormat, Names, Stack, Token } from '../../core';
import { ArnFormat, Names, Stack, Token, ValidationError } from '../../core';

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

this.fn.addPermission(`AllowInvoke:${Names.nodeUniqueId(topic.node)}`, {
Expand Down
8 changes: 4 additions & 4 deletions packages/aws-cdk-lib/aws-sns-subscriptions/lib/sqs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { SubscriptionProps } from './subscription';
import * as iam from '../../aws-iam';
import * as sns from '../../aws-sns';
import * as sqs from '../../aws-sqs';
import { ArnFormat, FeatureFlags, Names, Stack, Token } from '../../core';
import { ArnFormat, FeatureFlags, Names, Stack, Token, ValidationError } from '../../core';
import * as cxapi from '../../cx-api';

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

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

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

// add a statement to the queue resource policy which allows this topic
Expand Down
6 changes: 3 additions & 3 deletions packages/aws-cdk-lib/aws-sns-subscriptions/lib/url.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SubscriptionProps } from './subscription';
import * as sns from '../../aws-sns';
import { Token } from '../../core';
import { Token, UnscopedValidationError } from '../../core';

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

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

if (this.unresolvedUrl) {
Expand Down
Loading