Catch exceptions in API calls for the sake of old Chromium versions

Related feedback:
https://github.com/uBlockOrigin/uBlock-issues/issues/3217#issuecomment-2103628821
This commit is contained in:
Raymond Hill 2024-05-09 21:23:40 -04:00
parent 363ad6795c
commit bb479b0a66
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 23 additions and 15 deletions

View File

@ -24,29 +24,37 @@
const promisifyNoFail = function(thisArg, fnName, outFn = r => r) { const promisifyNoFail = function(thisArg, fnName, outFn = r => r) {
const fn = thisArg[fnName]; const fn = thisArg[fnName];
return function() { return function(...args) {
return new Promise(resolve => { return new Promise(resolve => {
fn.call(thisArg, ...arguments, function() { try {
if ( chrome.runtime.lastError instanceof Object ) { fn.call(thisArg, ...args, function(...args) {
void chrome.runtime.lastError.message; void chrome.runtime.lastError;
} resolve(outFn(...args));
resolve(outFn(...arguments));
}); });
} catch(ex) {
console.error(ex);
resolve(outFn());
}
}); });
}; };
}; };
const promisify = function(thisArg, fnName) { const promisify = function(thisArg, fnName) {
const fn = thisArg[fnName]; const fn = thisArg[fnName];
return function() { return function(...args) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fn.call(thisArg, ...arguments, function() { try {
fn.call(thisArg, ...args, function(...args) {
const lastError = chrome.runtime.lastError; const lastError = chrome.runtime.lastError;
if ( lastError instanceof Object ) { if ( lastError instanceof Object ) {
return reject(lastError.message); return reject(lastError.message);
} }
resolve(...arguments); resolve(...args);
}); });
} catch(ex) {
console.error(ex);
resolve();
}
}); });
}; };
}; };