Code review of IndexedDB usage for cache storage purpose

Use Promise.prototype.catch() to deal with potential exceptions.

Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/416
This commit is contained in:
Raymond Hill 2019-03-21 17:49:19 -03:00
parent bbdb08c74a
commit 26c57feee8
No known key found for this signature in database
GPG key ID: 25E1490B761470C2

View file

@ -144,14 +144,6 @@
let dbPromise; let dbPromise;
let dbTimer; let dbTimer;
const genericErrorHandler = function(ev) {
const error = ev.target && ev.target.error;
if ( error && error.name === 'QuotaExceededError' ) {
api.error = error.name;
}
console.error('[%s]', STORAGE_NAME, error && error.name);
};
const noopfn = function () { const noopfn = function () {
}; };
@ -216,20 +208,22 @@
return resolve(null); return resolve(null);
} }
req.onupgradeneeded = function(ev) { req.onupgradeneeded = function(ev) {
req = undefined; if ( ev.oldVersion === 1 ) { return; }
const db = ev.target.result; try {
db.onerror = db.onabort = genericErrorHandler; const db = ev.target.result;
const table = db.createObjectStore( const table = db.createObjectStore(
STORAGE_NAME, STORAGE_NAME,
{ keyPath: 'key' } { keyPath: 'key' }
); );
table.createIndex('value', 'value', { unique: false }); table.createIndex('value', 'value', { unique: false });
} catch(ex) {
req.onerror();
}
}; };
req.onsuccess = function(ev) { req.onsuccess = function(ev) {
if ( resolve === undefined ) { return; } if ( resolve === undefined ) { return; }
req = undefined; req = undefined;
db = ev.target.result; db = ev.target.result;
db.onerror = db.onabort = genericErrorHandler;
dbPromise = undefined; dbPromise = undefined;
resolve(db); resolve(db);
resolve = undefined; resolve = undefined;
@ -296,7 +290,7 @@
const visitAllFromDb = function(visitFn) { const visitAllFromDb = function(visitFn) {
getDb().then(db => { getDb().then(db => {
if ( !db ) { return visitFn(); } if ( !db ) { return visitFn(); }
const transaction = db.transaction(STORAGE_NAME); const transaction = db.transaction(STORAGE_NAME, 'readonly');
transaction.oncomplete = transaction.oncomplete =
transaction.onerror = transaction.onerror =
transaction.onabort = ( ) => visitFn(); transaction.onabort = ( ) => visitFn();
@ -333,6 +327,9 @@
keyvalStore[result.key] = result.value; keyvalStore[result.key] = result.value;
}) })
); );
}).catch(reason => {
console.info(`cacheStorage.getAllFromDb() failed: ${reason}`);
callback();
}); });
}; };