@@ -2,7 +2,7 @@ import type { SNSClient, SubscribeCommandInput } from '@aws-sdk/client-sns'
22import { SetSubscriptionAttributesCommand , SubscribeCommand } from '@aws-sdk/client-sns'
33import type { CreateQueueCommandInput , SQSClient } from '@aws-sdk/client-sqs'
44import type { STSClient } from '@aws-sdk/client-sts'
5- import { InternalError , stringValueSerializer } from '@lokalise/node-core'
5+ import { InternalError , isError , stringValueSerializer } from '@lokalise/node-core'
66import type { ExtraParams } from '@message-queue-toolkit/core'
77import type { ExtraSQSCreationParams } from '@message-queue-toolkit/sqs'
88import { 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
0 commit comments