Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/rare-pillows-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@segment/analytics-next': minor
---

Adds a new option `ignorePluginReadyError` which prevents `analytics.ready` failure when a plugin fails to be ready.
Also adds another option `onPluginReadyError` which allows developers to handle the plugin failures at ready stage.
51 changes: 51 additions & 0 deletions packages/browser/src/browser/__tests__/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,57 @@ describe('Initialization', () => {
expect(ready).toHaveBeenCalled()
})

it('ready method is called even when plugin errors on ready, and calls `onPluginReadyError`', async () => {
const ready = jest.fn()
const onPluginReadyError = jest.fn()

const lazyPlugin1: Plugin = {
name: 'Test 2',
type: 'destination',
version: '1.0',

load: async (_ctx) => {},
ready: async () => {
return new Promise((resolve) => setTimeout(resolve, 100))
},
isLoaded: () => true,
}

const lazyPlugin2: Plugin = {
name: 'Test 2',
type: 'destination',
version: '1.0',

load: async (_ctx) => {},
ready: () => Promise.reject('failed readying'),
isLoaded: () => true,
}

jest.spyOn(lazyPlugin1, 'load')
jest.spyOn(lazyPlugin2, 'load')
const [analytics] = await AnalyticsBrowser.load(
{
writeKey,
plugins: [lazyPlugin1, lazyPlugin2, xt],
},
{
onPluginReadyError,
}
)

// eslint-disable-next-line @typescript-eslint/no-floating-promises
const promise = analytics.ready(ready)
expect(lazyPlugin1.load).toHaveBeenCalled()
expect(lazyPlugin2.load).toHaveBeenCalled()
expect(ready).not.toHaveBeenCalled()

await sleep(100)
expect(ready).toHaveBeenCalled()
expect(onPluginReadyError).toHaveBeenCalledTimes(1)

return promise
})

describe('cdn', () => {
it('should get the correct CDN in plugins if the CDN overridden', async () => {
const overriddenCDNUrl = 'http://cdn.segment.com' // http instead of https
Expand Down
4 changes: 4 additions & 0 deletions packages/browser/src/browser/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ export interface InitOptions {
plan?: Plan
retryQueue?: boolean
obfuscate?: boolean

/** Error handling function for when an integration fails */
onPluginReadyError?: (error: Error) => Promise<void>

/**
* This callback allows you to update/mutate CDN Settings.
* This is called directly after settings are fetched from the CDN.
Expand Down
15 changes: 12 additions & 3 deletions packages/browser/src/core/analytics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,18 @@ export class Analytics
async ready(
callback: Function = (res: Promise<unknown>[]): Promise<unknown>[] => res
): Promise<unknown> {
return Promise.all(
this.queue.plugins.map((i) => (i.ready ? i.ready() : Promise.resolve()))
).then((res) => {
return Promise.allSettled(
this.queue.plugins.map((i) =>
i.ready
? i.ready().catch(this.options.onPluginReadyError)
: Promise.resolve()
)
).then((results) => {
const res = results.map((result) => {
if (result.status === 'fulfilled') {
return result.value
}
})
callback(res)
return res
})
Expand Down