diff --git a/docs/classes/EmbedClient.html b/docs/classes/EmbedClient.html index 69d243d..fff6fc8 100644 --- a/docs/classes/EmbedClient.html +++ b/docs/classes/EmbedClient.html @@ -6,7 +6,7 @@ embedded content.

Returns null | Promise<T>

Methods

Methods

Generated using TypeDoc

\ No newline at end of file +

Returns Promise<T>

Generated using TypeDoc

\ No newline at end of file diff --git a/src/embed.ts b/src/embed.ts index 271b3d1..9b9e235 100644 --- a/src/embed.ts +++ b/src/embed.ts @@ -264,10 +264,15 @@ export class EmbedClient { } EmbedClient.acquireSessionPromise = this.acquireCookielessEmbedSessionInternal() - return EmbedClient.acquireSessionPromise.then((url) => { - EmbedClient.sessionAcquired = true - return url - }) + return EmbedClient.acquireSessionPromise + .then((url) => { + EmbedClient.sessionAcquired = true + return url + }) + .catch((error) => { + EmbedClient.acquireSessionPromise = undefined + throw error + }) } private async acquireCookielessEmbedSessionInternal(): Promise { @@ -384,9 +389,12 @@ export class EmbedClient { this._connection = this.createIframe(this._builder.url) } else { if (this._builder.isCookielessEmbed) { - this._connection = this.acquireCookielessEmbedSession().then( - async (url) => this.createIframe(url) - ) + this._connection = this.acquireCookielessEmbedSession() + .then(async (url) => this.createIframe(url)) + .catch((_error) => { + this._connection = null + throw _error + }) } else { this._connection = this.createUrl().then(async (url) => this.createIframe(url) diff --git a/tests/embed.spec.ts b/tests/embed.spec.ts index 89d2ed7..aec648d 100644 --- a/tests/embed.spec.ts +++ b/tests/embed.spec.ts @@ -396,7 +396,6 @@ describe('LookerEmbed', () => { }) describe('receiving messages', () => { - let mockDashboardClient: any let embedDashboard: any const startFn = jasmine.createSpy('onStart').and.callFake(function () { @@ -410,7 +409,6 @@ describe('LookerEmbed', () => { ) { return Promise.resolve({}) }) - mockDashboardClient = {} builder = LookerEmbedSDK.createDashboardWithUrl(testUrl) builder.on('dashboard:run:start', startFn) client = builder.build() @@ -534,4 +532,67 @@ describe('LookerEmbed', () => { } }) }) + + describe('cookieless embed error', () => { + let fetchSpy: any + let fakeDashboardClient: any + const acquireData = [ + {}, + { + api_token: 'abcdef-api', + authentication_token: 'abcdef-auth', + navigation_token: 'abcdef-nav', + }, + ] + const acquire = () => { + const data = acquireData.splice(0, 1)[0] + return Promise.resolve(data) + } + // Not possible to test generate callback as it is tied to chatty + // which is overridden by the createIFrame spy. + const generate = () => + Promise.resolve({ + api_token: 'mnopqr-api', + navigation_token: 'mnopqr-nav', + }) + + beforeEach(() => { + fetchSpy = spyOn(window, 'fetch').and.returnValue({ + json: () => ({}), + ok: true, + status: 200, + }) + fakeDashboardClient = {} + builder = LookerEmbedSDK.createDashboardWithId(11) + client = builder.build() + spyOn(client, 'createIframe').and.returnValue( + Promise.resolve(fakeDashboardClient) + ) + }) + + afterEach(() => { + fetchSpy.calls.reset() + }) + + it('should allow connect to be called successfully after an error', async () => { + LookerEmbedSDK.initCookieless('host.looker.com:9999', acquire, generate) + try { + await client.connect() + fail() + return + } catch (error) { + expect(error.message).toEqual( + 'failed to prepare cookieless embed session' + ) + } + try { + await client.connect() + expect(client.createIframe).toHaveBeenCalledWith( + 'https://host.looker.com:9999/login/embed/%2Fembed%2Fdashboards%2F11%3Fembed_domain%3Dhttp%253A%252F%252Flocalhost%253A9876%26sdk%3D2%26embed_navigation_token%3Dabcdef-nav?embed_authentication_token=abcdef-auth' + ) + } catch (_error) { + fail() + } + }) + }) })