Skip to content

Commit

Permalink
fix(client): error handling for session connect
Browse files Browse the repository at this point in the history
  • Loading branch information
blakebyrnes committed Jan 6, 2021
1 parent 2f55df9 commit 82e58b8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 24 deletions.
38 changes: 28 additions & 10 deletions client/lib/Agent.ts
Expand Up @@ -17,6 +17,7 @@ import Request from 'awaited-dom/impl/official-klasses/Request';
import IWaitForOptions from '@secret-agent/core-interfaces/IWaitForOptions';
import { IElementIsolate } from 'awaited-dom/base/interfaces/isolate';
import CSSStyleDeclaration from 'awaited-dom/impl/official-klasses/CSSStyleDeclaration';
import { CanceledPromiseError } from '@secret-agent/commons/interfaces/IPendingWaitEvent';
import WebsocketResource from './WebsocketResource';
import IWaitForResourceFilter from '../interfaces/IWaitForResourceFilter';
import Resource from './Resource';
Expand Down Expand Up @@ -286,7 +287,8 @@ export default class Agent extends AwaitedEventTarget<{ close: void }> {
this.then = null;
return this;
})
.then(onfulfilled, onrejected);
.then(onfulfilled, onrejected)
.catch(onrejected);
}
}

Expand All @@ -295,7 +297,8 @@ class SessionConnection {
public hasConnected = false;

public get coreSession(): Promise<CoreSession> {
return this._coreSession ?? this.connectSession();
if (this._coreSession) return this.getCoreSessionOrReject();
return this.connectSession();
}

public get activeTab(): Tab {
Expand All @@ -307,14 +310,14 @@ class SessionConnection {
this._activeTab = value;
}

private _coreSession: Promise<CoreSession>;
private _coreSession: Promise<CoreSession | Error>;
private _activeTab: Tab;
private _tabs: Tab[] = [];

constructor(private agent: Agent) {}

public async refreshedTabs(): Promise<Tab[]> {
const session = await this.coreSession;
const session = await this.getCoreSessionOrReject();
const coreTabs = await session.getTabs();
const tabIds = await Promise.all(this._tabs.map(x => x.tabId));
for (const coreTab of coreTabs) {
Expand All @@ -328,7 +331,8 @@ class SessionConnection {
}

public async close(): Promise<void> {
const session = await this.coreSession;
if (!this.hasConnected) return;
const session = await this.getCoreSessionOrReject();
return session.close();
}

Expand All @@ -344,24 +348,38 @@ class SessionConnection {
this._tabs.push(tab);
}

private async getCoreSessionOrReject(): Promise<CoreSession> {
if (!this._coreSession) return undefined;
const session = await this._coreSession;
if (session instanceof Error) return Promise.reject(session);
return session;
}

private connectSession(): Promise<CoreSession> {
if (this.hasConnected) return this._coreSession;
if (this.hasConnected) {
return this.getCoreSessionOrReject();
}
this.hasConnected = true;
const { showReplay, coreConnection, ...options } = getState(this.agent).options;

const connection = createConnection(coreConnection ?? { isPersistent: false });

this._coreSession = connection.createSession(options);
this._coreSession = connection.createSession(options).catch(err => {
if (err instanceof CanceledPromiseError) return;
return err;
});

const session = this.getCoreSessionOrReject();

const defaultShowReplay = Boolean(JSON.parse(process.env.SA_SHOW_REPLAY ?? 'true'));

if (showReplay ?? defaultShowReplay) {
scriptInstance.launchReplay(options.sessionName, this._coreSession);
scriptInstance.launchReplay(options.sessionName, session);
}

const coreTab = this._coreSession.then(x => x.firstTab);
const coreTab = session.then(x => x.firstTab);
this._activeTab = createTab(this.agent, coreTab);
this._tabs = [this._activeTab];
return this._coreSession;
return session;
}
}
23 changes: 10 additions & 13 deletions client/lib/ScriptInstance.ts
Expand Up @@ -22,20 +22,17 @@ export default class ScriptInstance {
public launchReplay(sessionName: string, coreSession: Promise<CoreSession>): void {
// eslint-disable-next-line global-require
const { replay } = require('@secret-agent/replay/index');
coreSession
.then(session => {
return replay({
scriptInstanceId: this.id,
scriptStartDate: this.startDate,
sessionsDataLocation: session.sessionsDataLocation,
replayApiServer: session.replayApiServer,
sessionId: session.sessionId,
sessionName,
});
})
.catch(err => {
log.warn('Unable to connect to CoreTab', { error: err, sessionId: this.id });
// eslint-disable-next-line promise/catch-or-return
coreSession.then(session => {
return replay({
scriptInstanceId: this.id,
scriptStartDate: this.startDate,
sessionsDataLocation: session.sessionsDataLocation,
replayApiServer: session.replayApiServer,
sessionId: session.sessionId,
sessionName,
});
});
}

public generateSessionName(name: string, shouldCleanName = true): string {
Expand Down
2 changes: 1 addition & 1 deletion client/test/basic.test.ts
Expand Up @@ -44,7 +44,7 @@ describe('basic SecretAgent tests', () => {
});
}
}
const agent = new Agent({ coreConnection: new Piper() });
const agent = await new Agent({ coreConnection: new Piper() });
await agent.close();

const outgoingCommands = outgoing.mock.calls;
Expand Down

0 comments on commit 82e58b8

Please sign in to comment.