How to use error handler callback in Sails.js policy?

After asking this question , I found that I can add a callback array to the endpoint in the sails application, for example:

file: /api/policies/somepolicy.js

module.exports = thisIsAnArrayOfCallbacks;

This works fine, while each member thisIsAnArrayOfCallbacksis the functionone that accepts req, resand nextas arguments. The controller call performs all the functions in the array, and the expected result is obtained in the normal stream.

But when using a callback errorHandler(for example, in this example ) that accepts an additional parameter err, it doesn't work as expected: the app.get('/path', thisIsAnArrayOfCallbacks)expression - only version allows you errorHandlerto throw an exception and report the correct answer to the client, but when using the sail path, the function errorHandlerdoesn’t thrown, and an exception is thrown in response.

How can I get a parameter error catch an exception that occurred in one of the functions thisIsAnArrayOfCallbacksin order to send the client the proper answer (preferred user)?

Thanks in advance.

+4
source share
1 answer

, ; , . , try/catch, , - res.forbidden(), res.badRequest(), res.serverError() .. Sails v0.10 api/responses.

Sails, : 500 ( Sails v0.10 /api/responses/serverError, v0.9.x it config/500.js) ( 10.10) Express sails.config.express.loadMiddleware. . SO ( ) 500.

+4

Source: https://habr.com/ru/post/1530096/


All Articles