Skip to content

Commit 5ca8733

Browse files
committed
Cleanup
1 parent 9fee5a0 commit 5ca8733

File tree

5 files changed

+46
-16
lines changed

5 files changed

+46
-16
lines changed

packages/core/lib/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ export { type ParseMessageResult, parseMessage } from './utils/parseUtils.ts'
107107
export { objectToBuffer } from './utils/queueUtils.ts'
108108
export {
109109
isStartupResourcePollingEnabled,
110+
type PollingErrorCallback,
111+
type PollingErrorContext,
110112
type StartupResourcePollingCheckResult,
111113
StartupResourcePollingTimeoutError,
112114
type WaitForResourceOptions,

packages/core/lib/utils/startupResourcePollingUtils.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@ import { NO_TIMEOUT, type StartupResourcePollingConfig } from '../types/queueOpt
44

55
const DEFAULT_POLLING_INTERVAL_MS = 5000
66

7+
/**
8+
* Context passed to error callbacks indicating whether the error is final.
9+
*/
10+
export type PollingErrorContext = {
11+
/**
12+
* If true, the operation has stopped and will not retry.
13+
* If false, this is a transient error and the operation will continue.
14+
*/
15+
isFinal: boolean
16+
}
17+
18+
/**
19+
* Callback invoked when a polling operation fails.
20+
*/
21+
export type PollingErrorCallback = (error: Error, context: PollingErrorContext) => void
22+
723
export type StartupResourcePollingCheckResult<T> =
824
| {
925
isAvailable: true
@@ -54,7 +70,7 @@ export type WaitForResourceOptions<T> = {
5470
* This can happen due to polling timeout or unexpected errors during polling.
5571
* Only used when config.nonBlocking is true.
5672
*/
57-
onError?: (error: Error) => void
73+
onError?: PollingErrorCallback
5874
}
5975

6076
export class StartupResourcePollingTimeoutError extends Error {
@@ -314,7 +330,9 @@ export async function waitForResource<T>(
314330
resourceName,
315331
error,
316332
})
317-
onError?.(error)
333+
// isFinal: true because pollForResource only throws when it gives up
334+
// (timeout with throwOnTimeout: true, or unexpected error)
335+
onError?.(error, { isFinal: true })
318336
})
319337
})
320338

packages/sns/README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -598,10 +598,16 @@ const result = await initSnsSqs(
598598
console.log('Queue URL:', queueUrl)
599599
},
600600

601-
// Called if background polling fails (e.g., timeout)
602-
onResourcesError: (error) => {
603-
console.error('Failed to wait for resources:', error.message)
604-
// Handle error - maybe alert, retry, or graceful degradation
601+
// Called if background polling fails (e.g., timeout or unexpected error)
602+
onResourcesError: (error, context) => {
603+
if (context.isFinal) {
604+
// Polling has stopped and will not retry
605+
console.error('Failed to wait for resources:', error.message)
606+
// Handle error - maybe alert, retry, or graceful degradation
607+
} else {
608+
// Transient error, polling will continue
609+
console.warn('Transient error while polling:', error.message)
610+
}
605611
},
606612
},
607613
)

packages/sns/lib/utils/snsInitter.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { CreateTopicCommandInput, SNSClient } from '@aws-sdk/client-sns'
22
import type { CreateQueueCommandInput, SQSClient } from '@aws-sdk/client-sqs'
33
import type { STSClient } from '@aws-sdk/client-sts'
44
import { type Either, isError } from '@lokalise/node-core'
5-
import type { DeletionConfig, ExtraParams } from '@message-queue-toolkit/core'
5+
import type { DeletionConfig, ExtraParams, PollingErrorCallback } from '@message-queue-toolkit/core'
66
import {
77
isProduction,
88
isStartupResourcePollingEnabled,
@@ -35,7 +35,7 @@ async function pollForTopic(
3535
startupResourcePolling: NonNullable<SNSSQSQueueLocatorType['startupResourcePolling']>,
3636
extraParams?: ExtraParams,
3737
onResourceAvailable?: () => void,
38-
onError?: (error: Error) => void,
38+
onError?: PollingErrorCallback,
3939
): Promise<TopicPollingResult> {
4040
const topicResult = await waitForResource({
4141
config: startupResourcePolling,
@@ -132,7 +132,8 @@ async function createSubscriptionWithPolling(
132132
topicArn,
133133
error,
134134
})
135-
extraParams?.onResourcesError?.(error)
135+
// Subscription creation failure is final - we don't retry
136+
extraParams?.onResourcesError?.(error, { isFinal: true })
136137
}
137138
}
138139

@@ -181,7 +182,7 @@ async function pollForQueue(
181182
startupResourcePolling: NonNullable<SNSSQSQueueLocatorType['startupResourcePolling']>,
182183
extraParams?: ExtraParams,
183184
onResourceAvailable?: () => void,
184-
onError?: (error: Error) => void,
185+
onError?: PollingErrorCallback,
185186
): Promise<unknown | undefined> {
186187
return await waitForResource({
187188
config: startupResourcePolling,
@@ -210,7 +211,7 @@ export type InitSnsSqsExtraParams = ExtraParams & {
210211
* Callback invoked when background resource polling or subscription creation fails in non-blocking mode.
211212
* This can happen due to polling timeout or subscription creation failure.
212213
*/
213-
onResourcesError?: (error: Error) => void
214+
onResourcesError?: PollingErrorCallback
214215
}
215216

216217
export type InitSnsExtraParams = ExtraParams & {
@@ -339,8 +340,8 @@ export async function initSnsSqs(
339340
topicAvailable = true
340341
notifyIfBothReady()
341342
},
342-
(error) => {
343-
extraParams?.onResourcesError?.(error)
343+
(error, context) => {
344+
extraParams?.onResourcesError?.(error, context)
344345
},
345346
)
346347

@@ -366,8 +367,8 @@ export async function initSnsSqs(
366367
queueAvailable = true
367368
notifyIfBothReady()
368369
},
369-
(error) => {
370-
extraParams?.onResourcesError?.(error)
370+
(error, context) => {
371+
extraParams?.onResourcesError?.(error, context)
371372
},
372373
).then((result) => {
373374
// If queue was immediately available, pollForQueue returns the result

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ describe('SnsSqsPermissionConsumer - startupResourcePollingConfig', () => {
363363

364364
let errorCallbackInvoked = false
365365
let callbackError: Error | undefined
366+
let callbackContext: { isFinal: boolean } | undefined
366367

367368
const result = await initSnsSqs(
368369
sqsClient,
@@ -383,9 +384,10 @@ describe('SnsSqsPermissionConsumer - startupResourcePollingConfig', () => {
383384
undefined,
384385
undefined,
385386
{
386-
onResourcesError: (error) => {
387+
onResourcesError: (error, context) => {
387388
errorCallbackInvoked = true
388389
callbackError = error
390+
callbackContext = context
389391
},
390392
},
391393
)
@@ -408,6 +410,7 @@ describe('SnsSqsPermissionConsumer - startupResourcePollingConfig', () => {
408410

409411
expect(callbackError).toBeDefined()
410412
expect(callbackError?.message).toContain('Timeout')
413+
expect(callbackContext).toEqual({ isFinal: true })
411414
})
412415
})
413416

0 commit comments

Comments
 (0)