have better transaction errors
This commit is contained in:
parent
184480ad36
commit
bcfc4d1fd7
1 changed files with 17 additions and 2 deletions
|
@ -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();
|
||||
});
|
||||
});
|
||||
|
|
Reference in a new issue