I have a Node.js app to do some simple processing, however, every time there's an error, the server crashes.
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', function (exception) {
// handle or ignore error
console.log(exception);
});
setInterval(function() {
console.log('tick, even after error');
}, 1000);
a += 1;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.