have better transaction errors

This commit is contained in:
Bruno Windels 2021-05-05 16:02:39 +02:00
parent 184480ad36
commit bcfc4d1fd7

View file

@ -90,13 +90,28 @@ export function reqAsPromise(req) {
} }
export function txnAsPromise(txn) { export function txnAsPromise(txn) {
let error;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
txn.addEventListener("complete", () => { txn.addEventListener("complete", () => {
resolve(); resolve();
needsSyncPromise && Promise._flush && Promise._flush(); needsSyncPromise && Promise._flush && Promise._flush();
}); });
txn.addEventListener("abort", () => { txn.addEventListener("error", event => {
reject(new IDBRequestError(txn)); 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(); needsSyncPromise && Promise._flush && Promise._flush();
}); });
}); });