你如何捕捉 SailsJS 中的所有错误 [英] How do you catch all errors in SailsJS

查看:45
本文介绍了你如何捕捉 SailsJS 中的所有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Node 和 Sails 的新手,几个星期以来一直在努力解决这个问题.我需要记录(最终记录到文件或数据库)我们的 Sails 应用程序中发生的所有错误.我找到了几个接近解决这个问题的答案,但似乎没有一个 100% 有效.我根据 这个问题

I'm new to Node and Sails and have been struggling with this problem for a couple of weeks now. I need to log (eventually to a file or database) all errors that occur in our Sails app. I have found a couple of answers that come close to solving this, but none seem to work 100%. I have setup things based off of the answer from this question

使用 Sails v0.9.16 时,我在 config/500.js 文件中设置了我的日志记录,但是使用测试代码时...

When using Sails v0.9.16 I setup my logging in the config/500.js file, but when using test code...

t.t;

... 在我的控制器中,Sails 只是打印出ReferenceError: t is not defined".它永远不会转到 500.js.在 Sails v0.10.0-rc5 中,相同的测试代码将到达我的自定义 500 中间件(如下所示).

... in my controller, Sails just prints out "ReferenceError: t is not defined". It never goes to 500.js. In Sails v0.10.0-rc5 the same test code will get to my custom 500 middleware (shown below).

问题出在 Sails v0.10.0-rc5 中,中间件路由器似乎在访问我的自定义 400 中间件之前处理了 400 错误消息.

Problem is in Sails v0.10.0-rc5 it appears that the middleware Router handles 400 error message before getting to my custom 400 middleware.

我们还没有承诺将使用哪个版本,但让这个在一个或另一个中工作可能会让我们下定决心.

We have not committed to which version we are going to use, but getting this to work in one or the other would probably make up our minds.

那么我如何获得所有发生的错误?如果我走在正确的轨道上,我还缺少什么才能让它发挥作用?

So how do I get all errors that happen? If I am on the right track, what am I missing to get this to work?

提前致谢!

v0.9.16 没有太多代码可显示...我认为除了在 500.js 文件中添加sails.log 之外我没有做任何更改...

Not much code to show for v0.9.16...I don't think I changed anything other than adding a sails.log in the 500.js file...

以下是 v0.10.0-rc5 的自定义中间件设置...

Below is the custom middleware setup for v0.10.0-rc5 ...

    loadMiddleware: function(app, defaultMiddleware, sails) {

    // Use the middleware in the correct order
        app.use(defaultMiddleware.startRequestTimer);
        app.use(defaultMiddleware.cookieParser);
        app.use(defaultMiddleware.session);
        app.use(defaultMiddleware.bodyParser);
        app.use(defaultMiddleware.handleBodyParserError);
        app.use(defaultMiddleware.methodOverride);
        app.use(defaultMiddleware.poweredBy);

        app.use(defaultMiddleware.router); //400s do not make it past this...
        app.use(defaultMiddleware.www);
        app.use(defaultMiddleware.favicon);

        app.use(function(req, res, next ) {
            sails.log("400 error caught in middleware - " + err.stack);         
            next();
        });

        app.use(function(err, req, res, next){
            sails.log("500 error caught in middleware - " + err.stack);
            next(err);
        });
}

推荐答案

在 Sails v0.10 中,您有 自定义响应 来处理错误,因此您无需像示例中那样提供自定义中间件.默认情况下,大多数错误(即那些不是由 res.forbidden()res.notFound() 或其他处理程序专门触发的)将由 serverError 处理 位于 api/responses/serverError.js 中的响应.您可以自定义它以执行任何您喜欢的操作.

In Sails v0.10, you have custom responses to handle errors, so you don't need to provide custom middleware as in your example. By default, most errors (i.e. those not specifically triggered by res.forbidden(), res.notFound() or another handler) will be served by the serverError response that lives in api/responses/serverError.js. You can customize this to do whatever you like.

如果您已将 v0.9 应用升级到 v0.10,您可能没有 api/responses 文件夹.没问题;只需在空目录中使用 sails new nothing 生成一个新的 v0.10 项目并将 api/responses 文件夹从新项目复制到旧项目!

If you've upgraded a v0.9 app to v0.10, you might not have the api/responses folder. No problem; just use sails new whatever in an empty directory to generate a new v0.10 project and copy the api/responses folder from the new project to your old one!

这篇关于你如何捕捉 SailsJS 中的所有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆