From e722e1d0453e285ed06329bc335244ad5678b69b Mon Sep 17 00:00:00 2001 From: Piotr Kuczynski Date: Mon, 23 Nov 2026 17:00:48 +0100 Subject: [PATCH] Reject IndexedDB init instead of throwing when the upgrade fails Read the database from the open request, and fall back to the event target. If neither has one, or creating a store throws, reject the open promise and abort the version change transaction. Before, the error escaped onupgradeneeded as an uncaught exception on WebKit. Fixes #611 --- src/storage/indexed-db.js | 22 ++++++++++---- tests/unit/indexed-db.js | 61 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 5 deletions(-) diff --git a/src/storage/indexed-db.js b/src/storage/indexed-db.js index a6b6dcc9..9d594686 100644 --- a/src/storage/indexed-db.js +++ b/src/storage/indexed-db.js @@ -40,13 +40,25 @@ IDBStorageWrapper.prototype._openDb = function () { openRequest['transaction']['abort'](); return; } - var db = ev.target.result; + try { + var db = openRequest.result || (ev.target && ev.target.result); + if (!db) { + throw new Error('IndexedDB database unavailable during upgrade'); + } - storeNamesInDb.forEach(function (storeName) { - if (!db.objectStoreNames.contains(storeName)) { - db.createObjectStore(storeName); + storeNamesInDb.forEach(function (storeName) { + if (!db.objectStoreNames.contains(storeName)) { + db.createObjectStore(storeName); + } + }); + } catch (err) { + reject(err); + try { + openRequest['transaction']['abort'](); + } catch (abortErr) { + return; } - }); + } }; }); }; diff --git a/tests/unit/indexed-db.js b/tests/unit/indexed-db.js index 73fa6d41..69d4bd0c 100644 --- a/tests/unit/indexed-db.js +++ b/tests/unit/indexed-db.js @@ -67,6 +67,67 @@ describe(`IDBStorageWrapper`, function() { expect(transactionStub.callCount).to.equal(3); }); + describe(`when the upgrade fails`, function() { + const interceptUpgrade = (makeEvent, makeResult) => { + const open = window.indexedDB.open.bind(window.indexedDB); + sinon.stub(window.indexedDB, `open`).callsFake(function(name, version) { + const request = open(name, version); + let handler = null; + Object.defineProperty(request, `onupgradeneeded`, { + configurable: true, + get: () => handler && function(ev) { + if (makeResult) { + Object.defineProperty(request, `result`, {configurable: true, get: makeResult, set: () => {}}); + } + return handler.call(this, makeEvent(ev)); + }, + set: (fn) => { handler = fn; }, + }); + return request; + }); + }; + const uncaught = () => { + const errors = []; + const listener = (err) => errors.push(err); + process.on(`uncaughtException`, listener); + return {errors, stop: () => process.removeListener(`uncaughtException`, listener)}; + }; + + it(`reads the database from the request when the event has none`, async function() { + interceptUpgrade((ev) => ({oldVersion: ev.oldVersion, target: {}})); + + await idbWrapper.init(); + await idbWrapper.setItem(`test-key`, {foo: `bar`}); + + expect(await idbWrapper.getItem(`test-key`)).to.eql({foo: `bar`}); + }); + + it(`rejects init when no database is available`, async function() { + interceptUpgrade((ev) => ({oldVersion: ev.oldVersion, target: {}}), () => undefined); + const watcher = uncaught(); + + let error = null; + await idbWrapper.init().catch((err) => { error = err; }); + watcher.stop(); + + expect(error).to.be.an(`error`); + expect(watcher.errors).to.deep.equal([]); + }); + + it(`rejects init when creating a store throws`, async function() { + sinon.stub(window.IDBDatabase.prototype, `createObjectStore`).throws(new Error(`InvalidStateError`)); + const watcher = uncaught(); + + let error = null; + await idbWrapper.init().catch((err) => { error = err; }); + watcher.stop(); + + expect(error).to.be.an(`error`); + expect(error.message).to.equal(`InvalidStateError`); + expect(watcher.errors).to.deep.equal([]); + }); + }); + describe(`createIfMissing: false`, function() { const existingOnly = () => new IDBStorageWrapper( MIXPANEL_BROWSER_DB_NAME, RECORDING_REGISTRY_STORE_NAME, RECORDER_VERSION_DATA, {createIfMissing: false}