Skip to content

Commit 5df8b1f

Browse files
authored
Improve subscription error (#349)
* Improving subscription error * Release prepare
1 parent 5fbf16a commit 5df8b1f

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

packages/sns/lib/utils/snsSubscriber.ts

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { SNSClient, SubscribeCommandInput } from '@aws-sdk/client-sns'
22
import { SetSubscriptionAttributesCommand, SubscribeCommand } from '@aws-sdk/client-sns'
33
import type { CreateQueueCommandInput, SQSClient } from '@aws-sdk/client-sqs'
44
import type { STSClient } from '@aws-sdk/client-sts'
5-
import { InternalError, stringValueSerializer } from '@lokalise/node-core'
5+
import { InternalError, isError, stringValueSerializer } from '@lokalise/node-core'
66
import type { ExtraParams } from '@message-queue-toolkit/core'
77
import type { ExtraSQSCreationParams } from '@message-queue-toolkit/sqs'
88
import { assertQueue } from '@message-queue-toolkit/sqs'
@@ -85,38 +85,44 @@ export async function subscribeToTopic(
8585
queueArn,
8686
}
8787
} catch (err) {
88+
if (!isError(err)) throw err
89+
8890
const logger = extraParams?.logger ?? console
89-
logger.error(
90-
`Error while creating subscription for queue "${queueConfiguration.QueueName}", topic "${
91-
isCreateTopicCommand(topicConfiguration)
92-
? topicConfiguration.Name
93-
: topicConfiguration.topicName
94-
}": ${(err as Error).message}`,
95-
)
91+
const errMessage = `Error while creating subscription for queue "${queueConfiguration.QueueName}", topic "${
92+
isCreateTopicCommand(topicConfiguration)
93+
? topicConfiguration.Name
94+
: topicConfiguration.topicName
95+
}": ${err.message}`
96+
logger.error(errMessage)
9697

9798
if (
9899
subscriptionConfiguration.updateAttributesIfExists &&
99-
(err as Error).message.indexOf('Subscription already exists with different attributes') !== -1
100+
err.message.indexOf('Subscription already exists with different attributes') !== -1
100101
) {
101102
const result = await tryToUpdateSubscription(
102103
snsClient,
103104
topicArn,
104105
queueArn,
105106
subscriptionConfiguration,
106107
)
107-
if (!result) {
108-
logger.error('Failed to update subscription')
109-
throw err
110-
}
111-
return {
112-
subscriptionArn: result.SubscriptionArn,
113-
topicArn,
114-
queueUrl,
115-
queueArn,
108+
if (result) {
109+
return {
110+
subscriptionArn: result.SubscriptionArn,
111+
topicArn,
112+
queueUrl,
113+
queueArn,
114+
}
116115
}
116+
117+
logger.error('Failed to update subscription')
117118
}
118119

119-
throw err
120+
throw new InternalError({
121+
errorCode: 'sns_subscription_creation_failed',
122+
message: errMessage,
123+
details: { queueName: queueConfiguration.QueueName, topicArn, originalError: err.message },
124+
cause: err,
125+
})
120126
}
121127
}
122128

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": "23.1.3",
3+
"version": "23.1.4",
44
"private": false,
55
"license": "MIT",
66
"description": "SNS adapter for message-queue-toolkit",

0 commit comments

Comments
 (0)