Update comm message errors to include what the action was (#1312)

This commit is contained in:
toasted-nutbread 2021-01-25 18:44:49 -05:00 committed by GitHub
parent a14ff8f8cd
commit 5215c6b8b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -43,19 +43,27 @@ class CrossFrameAPIPort extends EventDispatcher {
invoke(action, params, ackTimeout, responseTimeout) { invoke(action, params, ackTimeout, responseTimeout) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (this._port === null) { if (this._port === null) {
reject(new Error('Port is disconnected')); reject(new Error(`Port is disconnected (${action})`));
return; return;
} }
const id = this._invocationId++; 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); this._activeInvocations.set(id, invocation);
if (ackTimeout !== null) { if (ackTimeout !== null) {
try { try {
invocation.timer = setTimeout(() => this._onError(id, new Error('Timeout (ack)')), ackTimeout); invocation.timer = setTimeout(() => this._onError(id, 'Acknowledgement timeout'), ackTimeout);
} catch (e) { } catch (e) {
this._onError(id, new Error('Failed to set timeout')); this._onError(id, 'Failed to set timeout');
return; return;
} }
} }
@ -79,7 +87,7 @@ class CrossFrameAPIPort extends EventDispatcher {
this._eventListeners.removeAllEventListeners(); this._eventListeners.removeAllEventListeners();
this._port = null; this._port = null;
for (const id of this._activeInvocations.keys()) { for (const id of this._activeInvocations.keys()) {
this._onError(id, new Error('Disconnected')); this._onError(id, 'Disconnected');
} }
this.trigger('disconnect', this); this.trigger('disconnect', this);
} }
@ -103,12 +111,12 @@ class CrossFrameAPIPort extends EventDispatcher {
_onAck(id) { _onAck(id) {
const invocation = this._activeInvocations.get(id); const invocation = this._activeInvocations.get(id);
if (typeof invocation === 'undefined') { 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; return;
} }
if (invocation.ack) { if (invocation.ack) {
this._onError(id, new Error(`Request ${id} already ack'd`)); this._onError(id, `Request ${id} already acknowledged`);
return; return;
} }
@ -122,9 +130,9 @@ class CrossFrameAPIPort extends EventDispatcher {
const responseTimeout = invocation.responseTimeout; const responseTimeout = invocation.responseTimeout;
if (responseTimeout !== null) { if (responseTimeout !== null) {
try { try {
invocation.timer = setTimeout(() => this._onError(id, new Error('Timeout (response)')), responseTimeout); invocation.timer = setTimeout(() => this._onError(id, 'Response timeout'), responseTimeout);
} catch (e) { } 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) { if (!invocation.ack) {
this._onError(id, new Error(`Request ${id} not ack'd`)); this._onError(id, `Request ${id} not acknowledged`);
return; return;
} }
@ -160,6 +168,10 @@ class CrossFrameAPIPort extends EventDispatcher {
const invocation = this._activeInvocations.get(id); const invocation = this._activeInvocations.get(id);
if (typeof invocation === 'undefined') { return; } if (typeof invocation === 'undefined') { return; }
if (typeof error === 'string') {
error = new Error(`${error} (${invocation.action})`);
}
this._activeInvocations.delete(id); this._activeInvocations.delete(id);
if (invocation.timer !== null) { if (invocation.timer !== null) {
clearTimeout(invocation.timer); clearTimeout(invocation.timer);