Promise.catch()

Syntax

promise.catch(function callback (error) {
    // Handle error
});

promise.catch(ErrorType, function callback (error) {
    // Handler error of ErrorType.
});

promise.catch(name, function callback (error) {
    // Handler error where error.name === name
});

Parameters

ErrorTypeConstructor function for the type of the error to catch
nameValue of the name property of the error to catch
callbackCallback to run when error occur

Return Value

The method will return another Promise that will resolve with the return value of given callback.

Description

Promise/A+ compliant catch() method. Enables caller to supply a callback to run if the promise fails. The return value of the catch callback will work as the resolve value for the returned promise.

This implementation also support to catch specific error types.

Dexie Specific

If you catch an operation you also tell Dexie that you are handling it which prohibit the transaction from aborting:

db.friends.add(newFriend).catch((error) => {
    // Error handled and transaction will not abort.
});

If you are catching an operation for logging purpose and do not want to mark it as “handled”, you could rethrow the error or return a rejected promis:

db.friends.add(newFriend).catch((error) => {
    console.error("Failed to add new friend. Error: " + error);
    return Promise.reject(error);
});

Catching at transaction level is another way to log without prohibiting transaction from aborting:

db.transaction('rw', db.friends, function () {
    db.friends.add(newFriend);
}).catch (function (error) {
    console.error ("Transaction aborted due to error: " + error);
});

Samples

Catching Error Events

var db = new Dexie('db');
db.version(1).stores({friends: 'email,name'});
db.open();

// Un-remark following line to make it fail due to ConstraintError:
//  db.friends.add({email: "[email protected]", name: "Oliver"}); 
db.friends.add({email: "[email protected]", name: "Gertrud"}).then(function() {
    alert ("Successfully added friend into DB");
}).catch (function (e) {
    alert ("Failed to add friend into DB: " + e);
});

Catching Thrown Exceptions

var db = new Dexie('db');
db.version(1).stores({friends: 'email,name'});
db.open();

db.friends.add({email: "[email protected]", name: "Gertrud"}).then(function() {
    throw new Error ("Ha ha ha!");
}).catch (function (e) {
    alert ("Failed: " + e.toString());
});

Catching Specific Error Types

var db = new Dexie('db');
db.version(1).stores({friends: 'email,name'});
db.open();

// Un-remark following line to make it fail due to ConstraintError:
//  db.friends.add({email: "[email protected]", name: "Oliver"}); 
db.friends.add({email: "[email protected]", name: "Gertrud"}).then(function() {
    alert ("Successfully added friend into DB");
}).catch (DOMError, function (e) {
    alert ("DOMError occurred: " + e);
}).catch (function (e) {
    alert ("Unknown error occurred: " + e);
});

Catching Error of Specific ‘name’

Sometimes the error type doesn’t tell exactly what error occurred. IndexedDB for example, will always fail with a DOMError, where its name property tells the actual reason. Dexie.Promise has support for supplying a string as first argument. By doing that, the name property of the error is checked against the given string.

var db = new Dexie('db');
db.version(1).stores({friends: 'email,name'});
db.open();

// Un-remark following line to make it fail due to ConstraintError:
//  db.friends.add({email: "[email protected]", name: "Oliver"}); 
db.friends.add({email: "[email protected]", name: "Gertrud"}).then(function() {
    alert ("Successfully added friend into DB");
}).catch ('ConstraintError', function (e) {
    alert ("ConstraintError occurred: " + e);
}).catch (function (e) {
    alert ("Unknown error occurred: " + e);
});

See Also

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

Table of Contents