如何处理 node.js 中的所有异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19909904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to handle all exceptions in node.js
提问by Pars
After working for a couple of weeks with node.js, I found that there is a difference between node.js server errors and regular server side languages like PHP.
在使用 node.js 工作几周后,我发现 node.js 服务器错误与常规服务器端语言(如 PHP)之间存在差异。
A simple example: IF an error happens in our website for ANY reason.
一个简单的例子:如果我们的网站由于任何原因发生错误。
in PHP
If a user send some invalid data to server and MySQL, MySQL will output error to that specific user and the whole application won't go down.
在 PHP 中
如果用户向服务器和 MySQL 发送了一些无效数据,MySQL 将向该特定用户输出错误,并且整个应用程序不会停机。
in Nodejs
If a user send some invalid data to server and MySQL, nodejs Server will go down and so all the users will disconnect and there is no connection between users anymore.
在 Nodejs 中,
如果用户向服务器和 MySQL 发送一些无效数据,nodejs 服务器将关闭,因此所有用户都将断开连接,用户之间不再有连接。
This is a really big problem. in large web applications, It is impossible to handle all errors to avoid Nodejs server to go down, and the question is,
Is there any way to handle any unknown fatal errors and exceptions to a specific output or something like it.
这真的是一个很大的问题。在大型 web 应用程序中,不可能处理所有错误以避免 Nodejs 服务器宕机,问题是,
有没有办法处理任何未知的致命错误和特定输出的异常或类似的东西。
回答by BadCanyon
You can use the uncaughtExceptionevent on the process object to do what you want, but like others have said, domains and catching/handling errors at the correct level is recommended.
您可以在流程对象上使用uncaughtException事件来执行您想要的操作,但就像其他人所说的那样,建议在正确级别捕获/处理域和错误。
process.on('uncaughtException', function(err) {
console.log('Caught exception: ' + err);
});
回答by Yonatan
You should simply validate the request data within your routes, catch any error (try-catch will work here since it's a sync operation) and handle it by returning appropriate HTTP status (e.g. 400) to the caller and log the error. If you're using Express you don't even have to use try-catch as Express will catch all synchronous exceptions and allow you to handle it centrally.
您应该简单地验证路由中的请求数据,捕获任何错误(try-catch 将在这里工作,因为它是同步操作)并通过向调用者返回适当的 HTTP 状态(例如 400)并记录错误来处理它。如果您使用 Express,您甚至不必使用 try-catch,因为 Express 将捕获所有同步异常并允许您集中处理它。
I personally don't think that catching validation errors using process.on('uncaughtException') is the best match for your need for two main reasons:
我个人认为使用 process.on('uncaughtException') 捕获验证错误不是最适合您的需求,原因有两个:
- At this location, you have no context of where the error took place and can't send back a response to the caller
- Any type of unhandled error will arrive here, if you've no idea what type of error occured it's recommended to restart the process. It doesn't make sense to restart the process because of invalid input, for example it's too easy this way to bring the app down for any external caller (DDOS)
- 在此位置,您没有发生错误的上下文,也无法向调用方发送响应
- 任何类型的未处理错误都会出现在这里,如果您不知道发生了什么类型的错误,建议重新启动该过程。由于输入无效而重新启动进程是没有意义的,例如,通过这种方式使任何外部调用者 (DDOS) 关闭应用程序太容易了
You may read here about other error handling best practicesand specifically refer to bullets 4,6 and 10
您可以在此处阅读有关其他错误处理最佳实践的信息,并特别参考第 4、6 和 10 条

