From bcfc4d1fd7b4b2933b0034b12d7e6215cb3de5d4 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Wed, 5 May 2021 16:02:39 +0200 Subject: [PATCH] have better transaction errors --- src/matrix/storage/idb/utils.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/matrix/storage/idb/utils.js b/src/matrix/storage/idb/utils.js index 4ecb04d6..2e6c2152 100644 --- a/src/matrix/storage/idb/utils.js +++ b/src/matrix/storage/idb/utils.js @@ -90,13 +90,28 @@ export function reqAsPromise(req) { } export function txnAsPromise(txn) { + let error; return new Promise((resolve, reject) => { txn.addEventListener("complete", () => { resolve(); needsSyncPromise && Promise._flush && Promise._flush(); }); - txn.addEventListener("abort", () => { - reject(new IDBRequestError(txn)); + txn.addEventListener("error", event => { + const request = event.target; + // catch first error here, but don't reject yet, + // as we don't have access to the failed request in the abort event handler + if (!error && request) { + error = new IDBRequestError(request); + } + }); + txn.addEventListener("abort", event => { + if (!error) { + const txn = event.target; + const dbName = txn.db.name; + const storeNames = Array.from(txn.objectStoreNames).join(", ") + error = new StorageError(`Transaction on ${dbName} with stores ${storeNames} was aborted.`); + } + reject(error); needsSyncPromise && Promise._flush && Promise._flush(); }); });