Skip to content

Commit edf1f95

Browse files
authored
Using InternalError to provide detailed info on topic exists issue (#228)
* Using InternalError to provide detailed info when the topic already exists * patch version
1 parent a16f810 commit edf1f95

File tree

4 files changed

+35
-20
lines changed

4 files changed

+35
-20
lines changed

packages/sns/lib/utils/snsUtils.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
type CreateTopicCommandInput,
3+
ListTagsForResourceCommand,
34
type SNSClient,
45
TagResourceCommand,
56
paginateListTopics,
@@ -13,7 +14,7 @@ import {
1314
SetTopicAttributesCommand,
1415
UnsubscribeCommand,
1516
} from '@aws-sdk/client-sns'
16-
import { type Either, isError } from '@lokalise/node-core'
17+
import { type Either, InternalError, isError } from '@lokalise/node-core'
1718
import { calculateOutgoingMessageSize as sqsCalculateOutgoingMessageSize } from '@message-queue-toolkit/sqs'
1819

1920
import type { ExtraSNSCreationParams } from '../sns/AbstractSnsService'
@@ -92,12 +93,26 @@ export async function assertTopic(
9293
const response = await snsClient.send(command)
9394
if (!response.TopicArn) throw new Error('No topic arn in response')
9495
topicArn = response.TopicArn
95-
} catch (err) {
96-
// We only manually build ARN in case of tag update
97-
if (!extraParams?.forceTagUpdate) throw err
96+
} catch (error) {
97+
if (!isError(error)) throw error
9898
// To build ARN we need topic name and error should be "topic already exist with different tags"
99-
if (!topicOptions.Name || !isTopicAlreadyExistWithDifferentTagsError(err)) throw err
99+
if (!topicOptions.Name || !isTopicAlreadyExistWithDifferentTagsError(error)) throw error
100+
100101
topicArn = await buildTopicArn(stsClient, topicOptions.Name)
102+
if (!extraParams?.forceTagUpdate) {
103+
const currentTags = await snsClient.send(
104+
new ListTagsForResourceCommand({ ResourceArn: topicArn }),
105+
)
106+
throw new InternalError({
107+
message: `${topicOptions.Name} - ${error.message}`,
108+
details: {
109+
currentTags: JSON.stringify(currentTags),
110+
newTags: JSON.stringify(topicOptions.Tags),
111+
},
112+
errorCode: 'SNS_TOPIC_ALREADY_EXISTS_WITH_DIFFERENT_TAGS',
113+
cause: error,
114+
})
115+
}
101116
}
102117

103118
if (extraParams?.queueUrlsWithSubscribePermissionsPrefix || extraParams?.allowedSourceOwner) {

packages/sns/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@message-queue-toolkit/sns",
3-
"version": "18.0.0",
3+
"version": "18.0.1",
44
"private": false,
55
"license": "MIT",
66
"description": "SNS adapter for message-queue-toolkit",

packages/sns/test/consumers/SnsSqsPermissionConsumer.spec.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -275,19 +275,19 @@ describe('SnsSqsPermissionConsumer', () => {
275275
})
276276

277277
await expect(consumer.init()).rejects.toThrowError(
278-
/Topic already exists with different tags/,
278+
`${topicNome} - Invalid parameter: Tags Reason: Topic already exists with different tags`,
279279
)
280280
})
281281

282282
it('updates existing queue and topic tags when update is forced', async () => {
283283
const initialTopicTags = [
284-
{ Key: 'project', Value: 'some-project' },
285-
{ Key: 'service', Value: 'some-service' },
286-
{ Key: 'leftover', Value: 'some-leftover' },
284+
{ Key: 'project', Value: 'sns-project' },
285+
{ Key: 'service', Value: 'sns-service' },
286+
{ Key: 'leftover', Value: 'sns-leftover' },
287287
]
288288
const newTopicTags = [
289-
{ Key: 'project', Value: 'some-project' },
290-
{ Key: 'service', Value: 'changed-service' },
289+
{ Key: 'project', Value: 'sns-project' },
290+
{ Key: 'service', Value: 'sns-service-changed' },
291291
{ Key: 'cc', Value: 'some-cc' },
292292
]
293293

@@ -299,13 +299,13 @@ describe('SnsSqsPermissionConsumer', () => {
299299
expect(preTopicTags.Tags).toEqual(initialTopicTags)
300300

301301
const initialQueueTags = {
302-
project: 'some-project',
303-
service: 'some-service',
304-
leftover: 'some-leftover',
302+
project: 'sqs-project',
303+
service: 'sqs-service',
304+
leftover: 'sqs-leftover',
305305
}
306306
const newQueueTags = {
307-
project: 'some-project',
308-
service: 'changed-service',
307+
project: 'sqs-project',
308+
service: 'sqs-service-changed',
309309
cc: 'some-cc',
310310
}
311311
const assertResult = await assertQueue(sqsClient, {
@@ -336,7 +336,7 @@ describe('SnsSqsPermissionConsumer', () => {
336336
const tags = postTopicTags.Tags
337337
expect(tags).toHaveLength(4)
338338
expect(postTopicTags.Tags).toEqual(
339-
expect.arrayContaining([...newTopicTags, { Key: 'leftover', Value: 'some-leftover' }]),
339+
expect.arrayContaining([...newTopicTags, { Key: 'leftover', Value: 'sns-leftover' }]),
340340
)
341341

342342
const updateQueueCall = sqsSpy.mock.calls.find((entry) => {
@@ -347,7 +347,7 @@ describe('SnsSqsPermissionConsumer', () => {
347347
const postQueueTags = await getQueueTags(assertResult.queueUrl)
348348
expect(postQueueTags.Tags).toEqual({
349349
...newQueueTags,
350-
leftover: 'some-leftover',
350+
leftover: 'sqs-leftover',
351351
})
352352
})
353353
})

packages/sns/test/publishers/SnsPermissionPublisher.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ describe('SnsPermissionPublisher', () => {
171171
})
172172

173173
await expect(newPublisher.init()).rejects.toThrowError(
174-
/Topic already exists with different tags/,
174+
`${topicNome} - Invalid parameter: Tags Reason: Topic already exists with different tags`,
175175
)
176176
})
177177
})

0 commit comments

Comments
 (0)