From 26c57feee89b3bc1f98c142222f1a153ec637640 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Thu, 21 Mar 2019 17:49:19 -0300 Subject: [PATCH] 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 --- src/js/cachestorage.js | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/src/js/cachestorage.js b/src/js/cachestorage.js index 317980813..f47cd805e 100644 --- a/src/js/cachestorage.js +++ b/src/js/cachestorage.js @@ -144,14 +144,6 @@ let dbPromise; 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 () { }; @@ -216,20 +208,22 @@ return resolve(null); } req.onupgradeneeded = function(ev) { - req = undefined; - const db = ev.target.result; - db.onerror = db.onabort = genericErrorHandler; - const table = db.createObjectStore( - STORAGE_NAME, - { keyPath: 'key' } - ); - table.createIndex('value', 'value', { unique: false }); + if ( ev.oldVersion === 1 ) { return; } + try { + const db = ev.target.result; + const table = db.createObjectStore( + STORAGE_NAME, + { keyPath: 'key' } + ); + table.createIndex('value', 'value', { unique: false }); + } catch(ex) { + req.onerror(); + } }; req.onsuccess = function(ev) { if ( resolve === undefined ) { return; } req = undefined; db = ev.target.result; - db.onerror = db.onabort = genericErrorHandler; dbPromise = undefined; resolve(db); resolve = undefined; @@ -296,7 +290,7 @@ const visitAllFromDb = function(visitFn) { getDb().then(db => { if ( !db ) { return visitFn(); } - const transaction = db.transaction(STORAGE_NAME); + const transaction = db.transaction(STORAGE_NAME, 'readonly'); transaction.oncomplete = transaction.onerror = transaction.onabort = ( ) => visitFn(); @@ -333,6 +327,9 @@ keyvalStore[result.key] = result.value; }) ); + }).catch(reason => { + console.info(`cacheStorage.getAllFromDb() failed: ${reason}`); + callback(); }); };