From a76d28daf592031e5741bea8ebfea6fdb349445d Mon Sep 17 00:00:00 2001 From: eight Date: Fri, 14 Dec 2018 14:06:48 +0800 Subject: [PATCH] Fix: make sure all errors are caught when initializing --- background/db.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/background/db.js b/background/db.js index c88af141..46fd0c9b 100644 --- a/background/db.js +++ b/background/db.js @@ -18,7 +18,7 @@ const db = (() => { }; function prepare() { - return shouldUseIndexedDB().then( + return withPromise(shouldUseIndexedDB).then( ok => { if (ok) { useIndexedDB(); @@ -37,16 +37,18 @@ const db = (() => { // which, once detected on the first run, is remembered in chrome.storage.local // for reliablility and in localStorage for fast synchronous access // (FF may block localStorage depending on its privacy options) + // note that it may throw when accessing the variable + // https://github.com/openstyles/stylus/issues/615 if (typeof indexedDB === 'undefined') { - return Promise.reject(new Error('indexedDB is undefined')); + throw new Error('indexedDB is undefined'); } // test localStorage const fallbackSet = localStorage.dbInChromeStorage; if (fallbackSet === 'true') { - return Promise.resolve(false); + return false; } if (fallbackSet === 'false') { - return Promise.resolve(true); + return true; } // test storage.local return chromeLocal.get('dbInChromeStorage') @@ -59,6 +61,14 @@ const db = (() => { }); } + function withPromise(fn) { + try { + return Promise.resolve(fn()); + } catch (err) { + return Promise.reject(err); + } + } + function testDBSize() { return dbExecIndexedDB('getAllKeys', IDBKeyRange.lowerBound(1), 1) .then(event => (