节点JS设置当uncaughtException发生时的消息

时间:2016-03-11 10:19:10

标签: javascript node.js

我对Node JS上的Gracefully Shutdown有疑问

我有设置方法,如果服务器上发生导致服务器停止工作的事情(错误的代码等),就会向开发人员发送自动邮件。

这些是我的代码的重点

process.on('uncaughtException', function (err) {
    console.error((new Date).toUTCString() + ' uncaughtException:', err.message);
    console.error(err.stack);
    //Mandrill
         ...
    });
});

我在电子邮件中获得的只是这些

TypeError: Cannot read property 'x' of undefined

但我想要的是我想在这样的邮件中包含日志

TypeError: Cannot read property 'x' of undefined
at ...\routes\v3\users.js:57:20
    at ...\node_modules\mongoose-deep-populate\lib\plugin.js:81:11
    at nextLevel (...\node_modules\mongoose-deep-populate\lib\plugin.js:237:36)
    at Promise.one (...\node_modules\mongoose-deep-populate\lib\plugin.js:232:27)
    at Promise.<anonymous> (...\node_modules\mongoose\node_modules\mpromise\lib\promise.js:177:8)
    at Promise.emit (events.js:107:17)
    at Promise.emit (...\node_modules\mongoose\node_modules\mpromise\lib\promise.js:84:38)
    at Promise.fulfill (...\node_modules\mongoose\node_modules\mpromise\lib\promise.js:97:20)
    at Promise.resolve (...\node_modules\mongoose\lib\promise.js:114:23)
    at ...\node_modules\mongoose\lib\model.js:2051:23

任何帮助?感谢

1 个答案:

答案 0 :(得分:1)

如果发生错误,我也会向自己邮寄错误。我的代码是

module.exports = function(error, req, res, next){
  if(error){
    var body = '<h3>Error stack </h3>'+ error.stack;
    sendMail(['me@myself.com'],'Ha Ha 500',body.replace(/(?:\r\n|\r|\n)/g, '<br />')) // replece function for formatting, sendMail() sends mail through amazon ses 
   }
  }

这是邮件的屏幕截图 enter image description here