简体   繁体   English

Node.js:任何错误都会导致Heroku服务器崩溃

[英]Node.js: Any error crashes Heroku server

I have a Node.js app to do some simple processing, however, every time there's an error, the server crashes. 我有一个Node.js应用程序来进行一些简单的处理,但是,每次出现错误时,服务器都会崩溃。

2011-12-19T19:27:49+00:00 app[web.1]: node.js:134
2011-12-19T19:27:49+00:00 app[web.1]:         throw e; // process.nextTick error, or 'error' event on first tick
2011-12-19T19:27:49+00:00 app[web.1]:         ^
2011-12-19T19:27:49+00:00 app[web.1]: TypeError: Object { customer_id: 4eef8ddb74019b9d4f00001d,
2011-12-19T19:27:49+00:00 app[web.1]:   date: Mon, 19 Dec 2011 19:27:36 GMT,
2011-12-19T19:27:49+00:00 app[web.1]:   callsid: '123',
2011-12-19T19:27:49+00:00 app[web.1]:   _id: 4eef902887e9770100000008 } has no method 'save'
2011-12-19T19:27:49+00:00 app[web.1]:     at Promise.<anonymous> (/app/src/call.js:137:24)
2011-12-19T19:27:49+00:00 app[web.1]:     at Promise.<anonymous> (/app/node_modules/mongoose/lib/promise.js:120:8)
2011-12-19T19:27:49+00:00 app[web.1]:     at Promise.<anonymous> (events.js:64:17)
2011-12-19T19:27:49+00:00 app[web.1]:     at Promise.emit (/app/node_modules/mongoose/lib/promise.js:59:38)
2011-12-19T19:27:49+00:00 app[web.1]:     at Promise.complete (/app/node_modules/mongoose/lib/promise.js:70:20)
2011-12-19T19:27:49+00:00 app[web.1]:     at /app/node_modules/mongoose/lib/query.js:738:28
2011-12-19T19:27:49+00:00 app[web.1]:     at model.init (/app/node_modules/mongoose/lib/model.js:181:36)
2011-12-19T19:27:49+00:00 app[web.1]:     at cb (/app/node_modules/mongoose/lib/query.js:736:14)
2011-12-19T19:27:49+00:00 app[web.1]:     at Array.<anonymous> (/app/node_modules/mongoose/lib/utils.js:394:16)
2011-12-19T19:27:49+00:00 app[web.1]:     at model.<anonymous> (/app/node_modules/mongoose/lib/document.js:181:5)

How do I protect from such errors? 如何防止此类错误? For example, when I call a variable that doesn't exist, the server crashes. 例如,当我调用一个不存在的变量时,服务器崩溃了。 I understand the error but sometimes I only notice the error when deployed and the server crashes. 我理解错误,但有时我只注意到部署时的错误,服务器崩溃。

You can make sure your script doesn't stop by catching the 'uncaughtException' with process.on('uncaughtException', callback) , like so: 您可以通过使用process.on('uncaughtException', callback) uncaughtException'来确保您的脚本不会停止,如下所示:

process.on('uncaughtException', function (exception) {
  // handle or ignore error
  console.log(exception);
});

setInterval(function() {
  console.log('tick, even after error');
}, 1000);

a += 1;

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

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