Edit Page

.tolerate()

Tolerate (swallow) the specified error, and return a new result value (or undefined) instead. (Don't throw.)

.tolerate(filter, handler)

Or:

Usage

#
Argument Type Details
1 filter String or Dictionary The code of the error that you want to intercept, or a dictionary of criteria for identifying the error to intercept.
2 handler Function? An optional procedural parameter, called automatically by Sails if the anticipated error is thrown. It receives the argument specified in the "Handler" usage table below. If specified, the handler should return a value that will be used as the result. If omitted, the anticipated error will be swallowed and the result of the query will be undefined.
Handler
#
Argument Type Details
1 err Error Your anticipated Error.

.tolerate() is useful for tolerating a kind of error (or all errors). If you chain on .tolerate() and it matches the error that occurs, then the underlying logic won't throw. Instead, it returns the return value of the handler function you passed into .tolerate().

Example

#

Say you're building an address book that doesn't allow records with duplicate email addresses. To instead swallow the error caused by entering a non-unique email address and update the existing contact:

let newOrExistingContact = await Contact.create({
  emailAddress,
  fullName
})
.fetch()
.tolerate('E_UNIQUE');

if(!newOrExistingContact) {
  newOrExistingContact = await Contact.updateOne({ emailAddress })
  .set({ fullName })
  .fetch();
}

Is something missing?

If you notice something we've missed or could be improved on, please follow this link and submit a pull request to the sails repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Reference

Reference