【问题标题】:uncaughtException then appendFile on error will this cause problems? NodeJsuncaughtException 然后 appendFile on error 这会导致问题吗? NodeJs
【发布时间】:2017-01-30 09:36:53
【问题描述】:

我很好奇 process.exit(1) 是否在 async 的回调中 操作,appendFile,如果原来的错误发生的地方会继续运行,并且处于不稳定状态。我试图将 process.exit(1) 与 fs 内联,但它不会写入。

将不胜感激创建将记录任何错误的错误记录的更好方法。 .

  const fs = require('fs');

  process.on('uncaughtException', e1 => {
    fs.appendFile('./logs/error.log', e1.stack, e2 => {
      //if (e1 || e2) console.log(e1,e2);

      process.exit(1)
    });

  })

【问题讨论】:

    标签: javascript node.js asynchronous error-handling


    【解决方案1】:

    看起来,您的程序每次以fs.appendFile 结束时都会以错误代码退出到控制台。 process.exit 应该杀死任何其他运行的东西。

    我可能会这样做:

    const fs = require('fs');
    
    process.on('uncaughtException', function (err) {
      fs.appendFile('./logs/error.log', err.stack, (fserr) => {
        if (err) console.log('FS ERROR', fserr); //now includes the fs error in log
        console.log(err); //maybe include error from uncaughtException?
        process.exit(1); //exit with error code
      });
    });
    

    【讨论】:

    • 我其实并不担心文件写入的错误,我只关心进程在错误发生后退出一点,而不是同时退出
    • 那要看还有什么情况了。在 .appendFile 完成之前,该过程不会退出。您可以尝试fs.appendFileSync,但如果 fs 在尝试附加到文件时出错,它将退出而不告诉您错误。
    • 嗯,这应该可以正常工作,因为这应该在发生其他任何事情之前发生,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2023-03-28
    • 2011-08-18
    • 2011-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-05
    • 1970-01-01
    相关资源
    最近更新 更多