files/en-us/web/api/idbrequest/error/index.md
{{ APIRef("IndexedDB") }} {{AvailableInWorkers}}
The error read-only property of the
{{domxref("IDBRequest")}} interface returns the error in the event of an unsuccessful
request.
A {{domxref("DOMException")}} or null if there is no error. The exception object will have one of the following names, depending on what caused the error.
These errors are asynchronous, meaning that they can't be handled via try...catch. However, if an IDBRequest has an {{domxref("IDBRequest.error_event", "error")}} event handler assigned, you can still inspect such errors by querying the request's error property via the event object, for example event.target.error.name or event.target.error.message.
AbortError
ConstraintError
NotReadableError
UnknownError
VersionError
Read errors occur when an IndexedDB stores values and then subsequently fails to read those values even though the associated records are still in the database.
Read errors can be one of two types — transient or unrecoverable:
Transient read errors are signalled by an UnknownError type, and are usually caused by low memory. This shouldn't be a problem for small databases. To avoid low memory situations in large databases, try to split up database access to only load the records you need at any one time, for example using specific key ranges relating to a user's search query or a pagination mechanism. If a low memory error is hit, the user may be asked to close other applications to free up space at the OS-level.
Unrecoverable read errors are signalled by a NotReadableError type, and are caused by source files being deleted.
For example, some browsers store large values (for example, audio file blobs for an offline podcast app) as separate files that are accessed via a reference stored in the database. It has been observed that these separate files can end up being deleted because they show up as opaque files to users when they are using disk space recovery programs, resulting in unrecoverable read errors when the IndexedDB is next accessed.
Possible corrective actions for unrecoverable read errors might include notifying the user, deleting the entry from the database, then attempting to re-fetch the data from the server.
InvalidStateError {{domxref("DOMException")}}
The following example requests a given record title, onsuccess gets the
associated record from the {{domxref("IDBObjectStore")}} (made available as
objectStoreTitleRequest.result), updates one property of the record, and then puts the
updated record back into the object store. Also included at the bottom is an
onerror function that reports what the error was if the request fails.
For a full working example, see our To-do Notifications app (View the example live).
const title = "Walk dog";
// Open up a transaction as usual
const objectStore = db
.transaction(["toDoList"], "readwrite")
.objectStore("toDoList");
// Get the to-do list with the specified title
const objectStoreTitleRequest = objectStore.get(title);
objectStoreTitleRequest.onsuccess = () => {
// Grab the data object returned as the result
const data = objectStoreTitleRequest.result;
// Update the notified value in the object to "yes"
data.notified = "yes";
// Create another request that inserts the item
// back into the database
const updateTitleRequest = objectStore.put(data);
// When this new request succeeds, run the displayData()
// function again to update the display
updateTitleRequest.onsuccess = () => {
displayData();
};
};
objectStoreTitleRequest.onerror = () => {
// If an error occurs with the request, log what it is
console.log(
`There has been an error with retrieving your data:
${objectStoreTitleRequest.error.name}: ${objectStoreTitleRequest.error.message}`,
);
};
{{Specifications}}
{{Compat}}