.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:

.intercept(handler)(to intercept all errors)

Usage

Argument

Type

Details

1

filter

or

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

or

A procedural parameter which Sails will call 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 also be provided. That's just shorthand-- it's the same thing as passing in a handler function that simply returns the string. (Convenient when using actions2.)

Handler

Argument

Type

Details

1

err

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 up to you -- it's determined by whatever 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 specific way so the appropriate message can be displayed to the end user. To intercept that error:

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-docs repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Built with Love

The Sails framework is maintained by a web & mobile studio in Austin, TX, with the help of our contributors. We created Sails in 2012 to assist us on Node.js projects. Naturally we open-sourced it. We hope it makes your life a little bit easier!