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) {
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();
});
});