IDBTransaction: onabort vs onerror

Viewed 225

When we want to handle IndexedDB errors on the transaction level, we have two options: onabort and onerror handlers.

What is the difference between them?

MDN isn't very helpful; it only says:

Consider using IDBTransaction.onabort instead to handle non- successful completion of the transaction.

Does this mean that onerror handler would handle only a subset of errors which would be handled by the onabort handler?

1 Answers

An error event is fired at a failed request (IDBRequest instance); it bubbles up from the request to the request's transaction (IDBTransaction) to the transaction's connection (IDBDatabase). At any point it can be logged and the default behavior (aborting the transaction) can be prevented if desired by calling preventDefault().

An abort event is fired at a transaction that has aborted. A complete event is fired a transaction that has successfully committed.

A transaction can fail for reasons unrelated to a particular request (e.g. quota exceeded, I/O error, etc). So if you want to be aware that a transaction has failed, watch for abort events. Similarly, because your logic may prevent the default behavior of a request error aborting the transaction, seeing an error event on a transaction does not tell you whether the transaction will eventually succeed or not.

Related