Node.js进程无一崩溃 [英] Nodejs process crashed without an exception

查看:66
本文介绍了Node.js进程无一崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的nodejs应用程序中发生未捕获的异常时,我会在日志中写入一个日志条目.

I write a log entry to the log when an uncaught exception occurred in my nodejs app.

process.on('uncaughtException', function (err) {
    log.warn('Caught exception. Exiting. error: ' + err + ", stack: " + err.stack);
    process.exit(1);
});

但是,今天有几次,该应用程序崩溃了,没有写入日志条目.

But, couple of times today, the app had crashed without writing a log entry.

两次处理类似请求时,应用程序都崩溃了,因此,这主要应归因于应用程序的某些问题,而不是因为有人杀死了它或其他一些外部因素.

Both times the app has crashed when processing a similar request, so it mostly should be due to some issue with the app, not because someone killed it or some other external factor.

那么,在没有给出未捕获异常的情况下,应用程序是否有可能崩溃?

So, is there any way the app could crash without giving an uncaught exception?

推荐答案

我建议您也添加 unhandledRejection .正如 Node.js 文档所述,

I will suggest you to add unhandledRejection as well. As the Node.js documentation is mentionning,

每当拒绝Promise且在事件循环之内没有错误处理程序附加到Promise时,都会发出 unhandledRejection 事件.当使用Promises进行编程时,异常被封装为拒绝的Promise".可以使用 promise.catch()捕获和处理拒绝,并通过Promise链进行传播. unhandledRejection 事件对于检测并跟踪已被拒绝但尚未处理拒绝的承诺很有用.

The unhandledRejection event is emitted whenever a Promise is rejected and no error handler is attached to the promise within a turn of the event loop. When programming with Promises, exceptions are encapsulated as "rejected promises". Rejections can be caught and handled using promise.catch() and are propagated through a Promise chain. The unhandledRejection event is useful for detecting and keeping track of promises that were rejected whose rejections have not yet been handled.

这里是一个如何再次使用Node.js提供的示例:

Here is an example of how to use it provided by Node.js again:

process.on('unhandledRejection', (reason, p) => {
  console.log('Unhandled Rejection at:', p, 'reason:', reason);
  // application specific logging, throwing an error, or other logic here
});

somePromise.then((res) => {
  return reportToUser(JSON.pasre(res)); // note the typo (`pasre`)
}); // no `.catch()` or `.then()`

这篇关于Node.js进程无一崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆