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
ErrorType | Constructor function for the type of the error to catch |
name | Value of the name property of the error to catch |
callback | Callback 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: "abc@def.com", name: "Oliver"});
db.friends.add({email: "abc@def.com", 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: "abc@def.com", 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: "abc@def.com", name: "Oliver"});
db.friends.add({email: "abc@def.com", 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 doesnt 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 cheched 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: "abc@def.com", name: "Oliver"});
db.friends.add({email: "abc@def.com", 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
- API Reference
- Access Control in Dexie Cloud
- Authentication in Dexie Cloud
- Best Practices
- Building Addons
- Collection
- Collection.and()
- Collection.clone()
- Collection.count()
- Collection.delete()
- Collection.desc()
- Collection.distinct()
- Collection.each()
- Collection.eachKey()
- Collection.eachPrimaryKey()
- Collection.eachUniqueKey()
- Collection.filter()
- Collection.first()
- Collection.keys()
- Collection.last()
- Collection.limit()
- Collection.modify()
- Collection.offset()
- Collection.or()
- Collection.primaryKeys()
- Collection.raw()
- Collection.reverse()
- Collection.sortBy()
- Collection.toArray()
- Collection.uniqueKeys()
- Collection.until()
- Compound Index
- Consistency in Dexie Cloud
- Consuming Dexie as a module
- DBCore
- DBCoreAddRequest
- DBCoreCountRequest
- DBCoreCursor
- DBCoreDeleteRangeRequest
- DBCoreDeleteRequest
- DBCoreGetManyRequest
- DBCoreGetRequest
- DBCoreIndex
- DBCoreKeyRange
- DBCoreMutateRequest
- DBCoreMutateResponse
- DBCoreOpenCursorRequest
- DBCorePutRequest
- DBCoreQuery
- DBCoreQueryRequest
- DBCoreQueryResponse
- DBCoreRangeType
- DBCoreSchema
- DBCoreTable
- DBCoreTableSchema
- DBCoreTransaction
- DBCoreTransactionMode
- DBPermissionSet
- Deprecations
- Derived Work
- Design
- Dexie
- Dexie Cloud CLI
- Dexie Cloud Docs
- Dexie Cloud REST API
- Dexie.AbortError
- Dexie.BulkError
- Dexie.ConstraintError
- Dexie.DataCloneError
- Dexie.DataError
- Dexie.DatabaseClosedError
- Dexie.IncompatiblePromiseError
- Dexie.InternalError
- Dexie.InvalidAccessError
- Dexie.InvalidArgumentError
- Dexie.InvalidStateError
- Dexie.InvalidTableError
- Dexie.MissingAPIError
- Dexie.ModifyError
- Dexie.NoSuchDatabaseErrorError
- Dexie.NotFoundError
- Dexie.Observable
- Dexie.Observable.DatabaseChange
- Dexie.OpenFailedError
- Dexie.PrematureCommitError
- Dexie.QuotaExceededError
- Dexie.ReadOnlyError
- Dexie.SchemaError
- Dexie.SubTransactionError
- Dexie.Syncable
- Dexie.Syncable.IDatabaseChange
- Dexie.Syncable.IPersistentContext
- Dexie.Syncable.ISyncProtocol
- Dexie.Syncable.StatusTexts
- Dexie.Syncable.Statuses
- Dexie.Syncable.registerSyncProtocol()
- Dexie.TimeoutError
- Dexie.TransactionInactiveError
- Dexie.UnknownError
- Dexie.UnsupportedError
- Dexie.UpgradeError()
- Dexie.VersionChangeError
- Dexie.VersionError
- Dexie.[table]
- Dexie.addons
- Dexie.async()
- Dexie.backendDB()
- Dexie.close()
- Dexie.currentTransaction
- Dexie.debug
- Dexie.deepClone()
- Dexie.defineClass()
- Dexie.delByKeyPath()
- Dexie.delete()
- Dexie.derive()
- Dexie.events()
- Dexie.exists()
- Dexie.extend()
- Dexie.fakeAutoComplete()
- Dexie.getByKeyPath()
- Dexie.getDatabaseNames()
- Dexie.hasFailed()
- Dexie.ignoreTransaction()
- Dexie.isOpen()
- Dexie.js
- Dexie.name
- Dexie.on()
- Dexie.on.blocked
- Dexie.on.close
- Dexie.on.error
- Dexie.on.populate
- Dexie.on.populate-(old-version)
- Dexie.on.ready
- Dexie.on.storagemutated
- Dexie.on.versionchange
- Dexie.open()
- Dexie.override()
- Dexie.semVer
- Dexie.setByKeyPath()
- Dexie.shallowClone()
- Dexie.spawn()
- Dexie.table()
- Dexie.tables
- Dexie.transaction()
- Dexie.transaction()-(old-version)
- Dexie.use()
- Dexie.verno
- Dexie.version
- Dexie.version()
- Dexie.vip()
- Dexie.waitFor()
- DexieCloudOptions
- DexieError
- Docs Home
- Download
- Export and Import Database
- Get started with Dexie in Angular
- Get started with Dexie in React
- Get started with Dexie in Svelte
- Get started with Dexie in Vue
- Hello World
- How To Use the StorageManager API
- Inbound
- IndexSpec
- Indexable Type
- IndexedDB on Safari
- Invite
- Migrating existing DB to Dexie
- MultiEntry Index
- Promise
- Promise.PSD
- Promise.catch()
- Promise.finally()
- Promise.on.error
- Promise.onuncatched
- Purchase Subscription
- Questions and Answers
- Releasing Dexie
- Road Map
- Road Map: Dexie 4.0
- Road Map: Dexie Cloud
- Run Dexie Cloud on Own Servers
- Sharding and Scalability
- Simplify with yield
- Stop Putin!
- SyncState
- Table
- Table Schema
- Table.add()
- Table.bulkAdd()
- Table.bulkDelete()
- Table.bulkGet()
- Table.bulkPut()
- Table.bulkUpdate()
- Table.clear()
- Table.count()
- Table.defineClass()
- Table.delete()
- Table.each()
- Table.filter()
- Table.get()
- Table.hook('creating')
- Table.hook('deleting')
- Table.hook('reading')
- Table.hook('updating')
- Table.limit()
- Table.mapToClass()
- Table.name
- Table.offset()
- Table.orderBy()
- Table.put()
- Table.reverse()
- Table.schema
- Table.toArray()
- Table.toCollection()
- Table.update()
- Table.where()
- The main limitations of IndexedDB
- Transaction
- Transaction.abort()
- Transaction.on.abort
- Transaction.on.complete
- Transaction.on.error
- Transaction.table()
- Tutorial
- Typescript
- Understanding the basics
- UserLogin
- Version
- Version.stores()
- Version.upgrade()
- WhereClause
- WhereClause.above()
- WhereClause.aboveOrEqual()
- WhereClause.anyOf()
- WhereClause.anyOfIgnoreCase()
- WhereClause.below()
- WhereClause.belowOrEqual()
- WhereClause.between()
- WhereClause.equals()
- WhereClause.equalsIgnoreCase()
- WhereClause.inAnyRange()
- WhereClause.noneOf()
- WhereClause.notEqual()
- WhereClause.startsWith()
- WhereClause.startsWithAnyOf()
- WhereClause.startsWithAnyOfIgnoreCase()
- WhereClause.startsWithIgnoreCase()
- db.cloud.configure()
- db.cloud.currentUser
- db.cloud.permissions()
- db.syncable.connect()
- db.syncable.delete()
- db.syncable.disconnect()
- db.syncable.getOptions()
- db.syncable.getStatus()
- db.syncable.list()
- db.syncable.on('statusChanged')
- db.syncable.setFilter()
- dexie-cloud-addon
- dexie-react-hooks
- liveQuery()
- unhandledrejection-event
- useLiveQuery()
- useObservable()
- usePermissions()