add a callstack to errors in browser and msg (#1369)

This commit is contained in:
tophf 2021-12-16 20:21:02 +03:00 committed by GitHub
parent 9d2854c272
commit 0ac01d2e22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 11 deletions

View File

@ -78,12 +78,12 @@
send(data, target = 'extension') { send(data, target = 'extension') {
return browser.runtime.sendMessage({data, target}) return browser.runtime.sendMessage({data, target})
.then(unwrapResponse); .then(unwrapResponseFactory('send'));
}, },
sendTab(tabId, data, options, target = 'tab') { sendTab(tabId, data, options, target = 'tab') {
return browser.tabs.sendMessage(tabId, {data, target}, options) return browser.tabs.sendMessage(tabId, {data, target}, options)
.then(unwrapResponse); .then(unwrapResponseFactory('sendTab'));
}, },
_execute(types, ...args) { _execute(types, ...args) {
@ -138,9 +138,16 @@
}; };
} }
function unwrapResponse({data, error} = {error: {message: ERR_NO_RECEIVER}}) { function unwrapResponseFactory(name) {
// Saving the local callstack before making an async call
return unwrapResponse.bind(null, new Error(`Callstack before invoking msg.${name}:`));
}
function unwrapResponse(localErr, {data, error} = {error: {message: ERR_NO_RECEIVER}}) {
return error return error
? Promise.reject(Object.assign(new Error(error.message), error)) ? Promise.reject(Object.assign(localErr, error, error.stack && {
stack: `${error.stack}\n${localErr.stack}`,
}))
: data; : data;
} }

View File

@ -23,14 +23,21 @@
}; };
const promisify = function (fn, ...args) { const promisify = function (fn, ...args) {
let res; let res;
let resolve, reject;
// Saving the local callstack before making an async call
const err = new Error();
try { try {
let resolve, reject; args.push((...results) => {
/* Some callbacks have 2 parameters so we're resolving as an array in that case. const {lastError} = chrome.runtime;
For example, chrome.runtime.requestUpdateCheck and chrome.webRequest.onAuthRequired */ if (lastError) {
args.push((...results) => err.message = lastError.message;
chrome.runtime.lastError ? reject(err);
reject(new Error(chrome.runtime.lastError.message)) : } else {
resolve(results.length <= 1 ? results[0] : results)); /* Some callbacks have 2 parameters so we're resolving as an array in that case.
For example, chrome.runtime.requestUpdateCheck and chrome.webRequest.onAuthRequired */
resolve(results.length <= 1 ? results[0] : results);
}
});
fn.apply(this, args); fn.apply(this, args);
res = new Promise((...rr) => ([resolve, reject] = rr)); res = new Promise((...rr) => ([resolve, reject] = rr));
} catch (err) { } catch (err) {