Edit Page

.intercept()

Capture and intercept the specified error, automatically modifying and re-throwing it, or specifying a new error to be thrown instead. (Still throws.)

.intercept(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. (If not provided, ALL errors will be intercepted.)
2 handler Function or String A procedural parameter which Sails calls automatically if the anticipated error is thrown. It will receive the argument specified in the "Handler" usage table below. The handler should return the modified Error, a new Error, or (if applicable) a special exit signal.

Alternatively, instead of a function, a string may be provided. This amounts to the same thing as passing in a handler function that simply returns the string. (Convenient when using actions2.)
Handler
#
Argument Type Details
1 err Error The anticipated Error being intercepted.

Return an Error instance or (if applicable) a special exit signal that will be thrown from the original logic instead of throwing the intercepted error.

.intercept() is for intercepting a certain kind of error (or all errors). If you chain on .intercept(), and it matches the error that occurs, then the underlying logic will throw. But what it throws is determined by what your handler function returns.

Example

#

If every user record in an app needs to have a unique email address, you may want to ensure that error is formatted in a such a way that the appropriate message will be displayed to the end user. To intercept that error:

var newUserRecord = await User.create({
  emailAddress: inputs.emailAddress,
  fullName: inputs.fullName,
})
.intercept('E_UNIQUE', ()=>{ return new Error('There is already an account using that email address!') })
.fetch();

Or, to handle the same error inside of an actions2 action, using a special exit signal; instead of an Error instance:

var newUserRecord = await User.create({
  emailAddress: inputs.emailAddress,
  fullName: inputs.fullName,
})
.intercept('E_UNIQUE', ()=>'emailAlreadyInUse')
.fetch();

Notes

#

Note that the usage in our example above could have also been written more concisely as:

.intercept('E_UNIQUE', 'emailAlreadyInUse')

Or less concisely as:

.intercept({ code: 'E_UNIQUE' }, ()=>{ return 'emailAlreadyInUse'; })

For more examples and further explanation of how .intercept() works, check out this related conversation.

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