本文介绍了如何在Sails.js策略中使用错误处理程序回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在询问,我发现我可以在一个sails应用程序中添加一个回调数组,如下所示:

After asking this question, I've found that I can add a callback array to an endpoint in a sails app like this:

file: /api/policies/somepolicy.js

module.exports = thisIsAnArrayOfCallbacks;

这个工作正常,而$ code的每个成员thisIsAnArrayOfCallbacks 是一个函数,它接受 req res code> next 作为参数。控制器调用执行数组中的所有功能,预期结果以正常流程获取。

This works ok while each member of thisIsAnArrayOfCallbacks is a function which accepts req, res, and next as arguments. The controller call executes all the functions in the array and the expected result is obtained in a normal flow.

但是当使用 errorHandler 回调(像),它需要一个额外的 err 参数,它不能按预期工作:express-only版本 app.get('/路径',thisIsAnArrayOfCallbacks)允许 errorHandler 获取异常并向客户端报告适当的响应,但是当使用sails方式时, code> errorHandler 函数未被调用,异常在响应中抛出。

But when using an errorHandler callback (like the one in this example) which takes an additional err parameter, it doesn't work as expected: the express-only version app.get('/path', thisIsAnArrayOfCallbacks) allows the errorHandler to fetch the exception and report a proper response to the client, but when using the sails way, the errorHandler function isn't called and the exception is thrown in the response.

我如何获取 err 参数或捕获异常发生在 thisIsAnArrayOfCallbacks 的函数之一发送一个适当的响应(自定义的一个是首选)客户端?

How could I fetch the err parameter or catch the exception occurred in one of the functions of thisIsAnArrayOfCallbacks to send a proper response (a custom one is preferred) to the client?

Th

推荐答案

你是正确的,因为政策不能被定义为错误回调;它们只是路由处理中间件,并且实际上绑定到应用于每个路由的每个路由。理想情况下,您可以使用 try / catch 在策略函数本身内捕获任何错误,并使用类似 res.forbidden() res.badRequest() res.serverError()等在Sails v0。 10,您可以进行任何自定义的回复,并将其存储在 api / responses 文件夹中。

You're correct in that policies can't be defined as error callbacks; they're solely route-handling middleware and are actually bound to each individual route that they are applied to. Ideally, you'd catch any errors within the policy functions themselves using try/catch and send a response using something like res.forbidden(), res.badRequest(), res.serverError(), etc. In Sails v0.10 you can make any custom response you want and store it in the api/responses folder.

如果真的要在Sails中实现一个全部错误处理程序,您有两个选择:覆盖默认的500处理程序(在Sails v0.10中,这是在 / api / responses / serverError ,在v0.9.x中,它是 config / 500.js )或(在v0.10中)创建自定义Express中间件,并使用 sails加载它.config.express.loadMiddleware 。有关详细信息,请参阅第二个选项,并记得在路由器之前添加您的自定义错误处理程序,然后在之前添加(或代替)500处理程序。

If you really want to implement a catch-all error handler in Sails you have two choices: either override the default 500 handler (in Sails v0.10 this is in /api/responses/serverError, in v0.9.x it's config/500.js), or (in v0.10) create custom Express middleware and load it using sails.config.express.loadMiddleware. See this SO question for details on the second option, and remember to add your custom error handler after the router and before (or in place of) the 500 handler.

这篇关于如何在Sails.js策略中使用错误处理程序回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 10:00
查看更多