Skip to content

Commit

Permalink
fix(client): correctly catch some canceled promise
Browse files Browse the repository at this point in the history
  • Loading branch information
blakebyrnes committed Feb 8, 2021
1 parent cc717e0 commit 1d5906f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
7 changes: 1 addition & 6 deletions client/lib/CoreCommandQueue.ts
Expand Up @@ -50,12 +50,7 @@ export default class CoreCommandQueue {
}

public clearPending(): void {
while (this.items.length) {
const next = this.items.shift();
const cancel = new CanceledPromiseError(`Canceling pending ${next.command} command`);
cancel.stack = next.stack;
next.reject();
}
this.items.length = 0;
}

// PRIVATE
Expand Down
3 changes: 2 additions & 1 deletion commons/Queue.ts
Expand Up @@ -30,7 +30,8 @@ export default class Queue {

public stop(): void {
while (this.queue.length) {
const next = this.queue.pop();
const next = this.queue.shift();
if (!next) continue;

this.reject(next, new CanceledPromiseError('Canceling Queue Item'));
}
Expand Down
12 changes: 8 additions & 4 deletions core/server/ConnectionToClient.ts
Expand Up @@ -10,6 +10,7 @@ import ICoreEventPayload from '@secret-agent/core-interfaces/ICoreEventPayload';
import IWaitForOptions from '@secret-agent/core-interfaces/IWaitForOptions';
import IAgentMeta from '@secret-agent/core-interfaces/IAgentMeta';
import Log from '@secret-agent/commons/Logger';
import { CanceledPromiseError } from '@secret-agent/commons/interfaces/IPendingWaitEvent';
import Session from '../lib/Session';
import Tab from '../lib/Tab';
import GlobalPool from '../lib/GlobalPool';
Expand Down Expand Up @@ -38,6 +39,8 @@ export default class ConnectionToClient extends TypedEventEmitter<{
// json converts args to null which breaks undefined argument handlers
const args = payload.args.map(x => (x === null ? undefined : x));

const session = meta?.sessionId ? Session.get(meta.sessionId) : undefined;

let data: any;
let isError = false;
try {
Expand All @@ -58,6 +61,10 @@ export default class ConnectionToClient extends TypedEventEmitter<{
}
}
} catch (error) {
// if we're closing, don't emit errors
if ((this.isClosing || session?.isClosing) && error instanceof CanceledPromiseError) {
return;
}
isError = true;
data =
error instanceof Error
Expand All @@ -69,10 +76,7 @@ export default class ConnectionToClient extends TypedEventEmitter<{
: new Error(`Unknown error occurred ${error}`);
}

let commandId: number;
if (meta?.sessionId) {
commandId = Session.get(meta.sessionId)?.sessionState?.lastCommand?.id;
}
const commandId = session?.sessionState?.lastCommand?.id;

const response: ICoreResponsePayload = {
responseId: messageId,
Expand Down

0 comments on commit 1d5906f

Please sign in to comment.