From feabada2d86cd078ebaa5db9fbaee30b449a70e6 Mon Sep 17 00:00:00 2001 From: Igor Savin Date: Thu, 22 Feb 2024 15:41:05 +0200 Subject: [PATCH] Handle queue not existing correctly --- ...sSqsPermissionsConsumerMultiSchema.spec.ts | 31 ++++++++++++++++++ packages/sqs/lib/utils/sqsUtils.ts | 32 ++++++++++++------- 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/packages/sns/test/consumers/SnsSqsPermissionsConsumerMultiSchema.spec.ts b/packages/sns/test/consumers/SnsSqsPermissionsConsumerMultiSchema.spec.ts index dfd1a933..edbbf175 100644 --- a/packages/sns/test/consumers/SnsSqsPermissionsConsumerMultiSchema.spec.ts +++ b/packages/sns/test/consumers/SnsSqsPermissionsConsumerMultiSchema.spec.ts @@ -108,6 +108,37 @@ describe('SNS PermissionsConsumerMultiSchema', () => { expect(attributes.result?.attributes!.KmsMasterKeyId).toBe('othervalue') }) + + it('does not attempt to update non-existing queue when passing update param', async () => { + const newConsumer = new SnsSqsPermissionConsumerMultiSchema(diContainer.cradle, { + creationConfig: { + topic: { + Name: 'sometopic', + }, + queue: { + QueueName: 'existingQueue', + Attributes: { + KmsMasterKeyId: 'othervalue', + }, + }, + updateAttributesIfExists: true, + }, + deletionConfig: { + deleteIfExists: false, + }, + }) + + await newConsumer.init() + expect(newConsumer.queueUrl).toBe( + 'http://sqs.eu-west-1.localstack:4566/000000000000/existingQueue', + ) + + const attributes = await getQueueAttributes(sqsClient, { + queueUrl: newConsumer.queueUrl, + }) + + expect(attributes.result?.attributes!.KmsMasterKeyId).toBe('othervalue') + }) }) describe('consume', () => { diff --git a/packages/sqs/lib/utils/sqsUtils.ts b/packages/sqs/lib/utils/sqsUtils.ts index 46b28513..832bd867 100644 --- a/packages/sqs/lib/utils/sqsUtils.ts +++ b/packages/sqs/lib/utils/sqsUtils.ts @@ -26,20 +26,30 @@ export async function getQueueUrl( sqsClient: SQSClient, queueName: string, ): Promise> { - const result = await sqsClient.send( - new GetQueueUrlCommand({ - QueueName: queueName, - }), - ) + try { + const result = await sqsClient.send( + new GetQueueUrlCommand({ + QueueName: queueName, + }), + ) - if (result.QueueUrl) { - return { - result: result.QueueUrl, + if (result.QueueUrl) { + return { + result: result.QueueUrl, + } } - } - return { - error: 'not_found', + return { + error: 'not_found', + } + } catch (err) { + // @ts-ignore + if (err.Code === 'AWS.SimpleQueueService.NonExistentQueue') { + return { + error: 'not_found', + } + } + throw err } }