简体   繁体   English

NodeJS域错误处理未处理ReferenceError

[英]NodeJS domain error handling not picking up ReferenceError

I am having issues with domains in NodeJS. 我在NodeJS中遇到域名问题。 I have set up some middleware functions in a way that a domain is wrapping every call I make in my web application. 我已经以域将我在Web应用程序中进行的每个调用包装的方式设置了一些中间件功能。 My aim is to be able to pick up any error that happens, process it, and return a helpful message about the error that happened. 我的目标是能够发现所有发生的错误,进行处理,并返回有关发生的错误的有用消息。

This works for most errors, but I am seeing that the domain is either not seeing, or triggering my event handler on ReferenceError. 这适用于大多数错误,但我看到该域未看到或在ReferenceError上触发了事件处理程序。 I've tried many things, and looked around many places but to no avail. 我尝试了很多事情,环顾了许多地方,但无济于事。 I thought there might be a difference between an Error and an Exception, but that's unconfirmed. 我以为“错误”和“异常”之间可能会有区别,但这尚未得到证实。

I noticed that since I am listening on the Error event, maybe such an event is not triggered on some types of errors like ReferenceError. 我注意到,由于我正在侦听Error事件,因此可能不会在某些类型的错误(如ReferenceError)上触发此类事件。

I've tried wrapping my code in process.nextTick as well putting erroneous code inside a async.js block. 我尝试将我的代码包装在process.nextTick中,并将错误代码放入async.js块中。

I've tried using https://github.com/brianc/node-domain-middleware as a fix. 我尝试使用https://github.com/brianc/node-domain-middleware作为修复程序。

I've tried listening on the error event for a domain, and listening on an EventEmitter and adding that emitter to the domain. 我试过侦听域的错误事件,并侦听EventEmitter并将该发射器添加到域中。

As well as, using the enter and exit methods of the Domain object itself. 以及使用Domain对象本身的enter和exit方法。 I'm sure there are more things I can't exactly remember right now. 我敢肯定,现在还有更多我不记得的事情。

Really, all I'm looking for, is a solution that will catch any error that happens in my nodejs web application, and allows me to process the error that occurs. 我真正想要的是一个解决方案,该解决方案可以捕获在nodejs Web应用程序中发生的任何错误,并允许我处理所发生的错误。

There is an overarching middleware that executes everytime, and middleware functioning only on api calls. 每次都执行一个总体的中间件,并且中间件仅在api调用上起作用。 The middleware for the api calls check to see if you are authenticated (which works and is validated in another part of the app). api调用的中间件检查您是否已通过身份验证(该方法有效且已在应用程序的另一部分进行了验证)。

Yes, I have tried creating a domain for every request inside both of these middlewares, and I have tried creating and placing these error_handling_domain.run statements in every order and combination I could think of between these two middlewares. 是的,我尝试为这两种中间件中的每个请求创建一个域,并且尝试按我认为在这两种中间件之间的每种顺序和组合来创建和放置这些error_handling_domain.run语句。 Every time it's always the same, no output or signal that the error handlers have been triggered. 每次总是相同时,不会输出或发出错误处理程序已触发的信号。

the code: 编码:

  var error_handling_domain = domain.create(); var EventEmitter = require('events').EventEmitter; var emitter = new EventEmitter(); error_handling_domain.on('error', function(er) { console.log("i caught an error"); console.log(er); }); emitter.on('error', function(err) { console.log("I am an emitter error"); console.log(err); }); error_handling_domain.add(emitter); app.use(function(req, res, next) { error_handling_domain.run(function() { next(); }); }); app.all('/api/*', function(req, res, next) { var original_url_prefix = req.originalUrl.split('/')[1]; original_url_prefix = (original_url_prefix) ? original_url_prefix.toLowerCase() : ""; req.user = "me"; var authentication_required = original_url_prefix === 'api' && !req.user; if (authentication_required) { res.sendStatus(401); } else { next(); } }); app.post('/api/my_route/here', handle_post); function handle_post(req, resp){ switch(req.body.action){ case 'download': this.download_report(req.body, resp); } } function download_report(params, resp){ wellhi.thing; resp.json({success: "I guess"}); } 

Assuming that I make a post to '/api/my_route/here', download_report is called and tries to reference wellhi.thing which is clearly not a real thing. 假设我在“ / api / my_route / here”发布了一个帖子,则会调用download_report并尝试引用wellhi.thing,这显然不是真实的东西。 That's the test error code I'm using. 那就是我正在使用的测试错误代码。

I am using NodeJS, and expressjs. 我正在使用NodeJS和expressjs。 I have been trying to get intimately familiar with the following pages: 我一直在试图与以下页面非常熟悉:

https://nodejs.org/api/errors.html#errors_class_referenceerror https://nodejs.org/api/errors.html#errors_class_referenceerror

http://tuttlem.github.io/2015/05/16/handling-exceptions-with-domains-in-nodejs.html http://tuttlem.github.io/2015/05/16/handling-exceptions-with-domains-in-nodejs.html

https://strongloop.com/strongblog/robust-node-applications-error-handling/ https://strongloop.com/strongblog/robust-node-applications-error-handling/

https://nodejs.org/api/domain.html http://www.lighthouselogic.com/node-domains-as-a-replacement-for-try-catch/ https://nodejs.org/api/domain.html http://www.lighthouselogic.com/node-domains-as-a-replacement-for-try-catch/

Stackoverflow posts I looked at: 我看过的Stackoverflow帖子:

Domains not properly catching errors while testing nodeJS in mocha 在Mocha中测试NodeJS时,域无法正确捕获错误

NodeJS Domains and expressjs NodeJS域和expressjs

Please let me know if there's more information I can add to this to help clarify. 请让我知道是否有更多信息可以添加以帮助澄清。

I've accepted that there doesn't seem to be a way to completely handle every error using Domains, sadly. 我已经接受了,遗憾的是,似乎没有一种方法可以完全解决使用Domains的所有错误。

What I've done is far from what I wanted - I've set up an expressjs error handling route specifically after other routes. 我所做的事情远非我想要的-我已经在其他路由之后专门设置了expressjs错误处理路由。 I've isolated the error handling to its own function, and both the Domain and expressjs route point to that error handling function. 我已将错误处理隔离到其自己的功能,并且Domain和expressjs路由均指向该错误处理功能。

For whatever reason, this seems to catch the unexpected errors (like ReferenceError) that Domains refuse to catch. 无论出于何种原因,这似乎都捕获了域拒绝捕获的意外错误(例如ReferenceError)。 I am therefore, using both error catches at the same time. 因此,我同时使用了两个错误捕获。 I am a bit concerned about possible overlap and potential conflicts with this approach. 我对此方法可能存在的重叠和潜在的冲突感到有些担忧。

Purely conjecture: It seems like some errors do not emit an error event, which could be a reason Domains were not triggering. 纯粹是推测:似乎某些错误不会发出错误事件,这可能是Domains未触发的原因。

The strategy has then been: 当时的策略是:

  • Domains: errors I can predict // specifically throw // watch for 域:我可以预测的错误//专门抛出//注意
  • Expressjs: unexpected errors I cannot predict Express.js:我无法预测的意外错误

The code: 编码:

 function error_handling(err, req, res) { console.log("hello im an error."); } var error_handling_domain = domain.create(); error_handling_domain.on("error", error_handling); app.use(function(req, res, next) { error_handling_domain.run(function(){ next(); }); }); app.use('/api', function(req, res, next){ var authentication_required = <check_for_auth>; if(authentication_required) { res.sendStatus(401); } else { next(); } }); var routes = require('routes.js'); routes.initialize(app); app.use(function(err, req, res, next) { error_handling(err, req, res) }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM