From 5215c6b8b476d491f324ad3a285bdf6b66a5de41 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Mon, 25 Jan 2021 18:44:49 -0500 Subject: [PATCH] Update comm message errors to include what the action was (#1312) --- ext/mixed/js/comm.js | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/ext/mixed/js/comm.js b/ext/mixed/js/comm.js index 4a6786c3..ea450da6 100644 --- a/ext/mixed/js/comm.js +++ b/ext/mixed/js/comm.js @@ -43,19 +43,27 @@ class CrossFrameAPIPort extends EventDispatcher { invoke(action, params, ackTimeout, responseTimeout) { return new Promise((resolve, reject) => { if (this._port === null) { - reject(new Error('Port is disconnected')); + reject(new Error(`Port is disconnected (${action})`)); return; } const id = this._invocationId++; - const invocation = {id, resolve, reject, responseTimeout, ack: false, timer: null}; + const invocation = { + id, + resolve, + reject, + responseTimeout, + action, + ack: false, + timer: null + }; this._activeInvocations.set(id, invocation); if (ackTimeout !== null) { try { - invocation.timer = setTimeout(() => this._onError(id, new Error('Timeout (ack)')), ackTimeout); + invocation.timer = setTimeout(() => this._onError(id, 'Acknowledgement timeout'), ackTimeout); } catch (e) { - this._onError(id, new Error('Failed to set timeout')); + this._onError(id, 'Failed to set timeout'); return; } } @@ -79,7 +87,7 @@ class CrossFrameAPIPort extends EventDispatcher { this._eventListeners.removeAllEventListeners(); this._port = null; for (const id of this._activeInvocations.keys()) { - this._onError(id, new Error('Disconnected')); + this._onError(id, 'Disconnected'); } this.trigger('disconnect', this); } @@ -103,12 +111,12 @@ class CrossFrameAPIPort extends EventDispatcher { _onAck(id) { const invocation = this._activeInvocations.get(id); if (typeof invocation === 'undefined') { - yomichan.logWarning(new Error(`Request ${id} not found for ack`)); + yomichan.logWarning(new Error(`Request ${id} not found for acknowledgement`)); return; } if (invocation.ack) { - this._onError(id, new Error(`Request ${id} already ack'd`)); + this._onError(id, `Request ${id} already acknowledged`); return; } @@ -122,9 +130,9 @@ class CrossFrameAPIPort extends EventDispatcher { const responseTimeout = invocation.responseTimeout; if (responseTimeout !== null) { try { - invocation.timer = setTimeout(() => this._onError(id, new Error('Timeout (response)')), responseTimeout); + invocation.timer = setTimeout(() => this._onError(id, 'Response timeout'), responseTimeout); } catch (e) { - this._onError(id, new Error('Failed to set timeout')); + this._onError(id, 'Failed to set timeout'); } } } @@ -137,7 +145,7 @@ class CrossFrameAPIPort extends EventDispatcher { } if (!invocation.ack) { - this._onError(id, new Error(`Request ${id} not ack'd`)); + this._onError(id, `Request ${id} not acknowledged`); return; } @@ -160,6 +168,10 @@ class CrossFrameAPIPort extends EventDispatcher { const invocation = this._activeInvocations.get(id); if (typeof invocation === 'undefined') { return; } + if (typeof error === 'string') { + error = new Error(`${error} (${invocation.action})`); + } + this._activeInvocations.delete(id); if (invocation.timer !== null) { clearTimeout(invocation.timer);