捕获所有未处理的 javascript 承诺拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31472439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:44:40  来源:igfitidea点击:

Catch all unhandled javascript promise rejections

javascriptes6-promise

提问by Peter Mols

I would like to catch all unhandled exceptions/rejections that take place within a javascript Promise. Is there a good method for catching them without adding a .catch(..)on each end of the Promise chain? (in case of forgetting to add this, the error silently disappears).

我想捕获在 javascript Promise 中发生的所有未处理的异常/拒绝。有没有一种很好的方法可以在不在.catch(..)Promise 链的每一端添加 a 的情况下捕获它们?(如果忘记添加这个,错误会悄悄消失)。

The developer console in Google Chrome can log them, I like to log them as well in a production environment.

Google Chrome 中的开发者控制台可以记录它们,我也喜欢在生产环境中记录它们。

For normal javascript exceptions I use the window.onerrorfunction, but the errors from a Promise call this function.

对于普通的 javascript 异常,我使用该window.onerror函数,但来自 Promise 的错误调用此函数。

Example:

例子:

window.onerror = function (e) {
    alert("unhandled error: " + e);
};

var p = new Promise(function (resolve, reject) {
    var nullObject = null;
    // Raise a TypeError: Cannot read property 'forceNullError' of null
    var x = nullObject.forceNullError(); 
    resolve();
});

p.then(function () { alert('success'); });

JSFiddle: https://jsfiddle.net/f7zwej6L/

JSFiddle:https://jsfiddle.net/f7zwej6L/

*) I noticed that WinJS has a .done(..)method for what I want, but Native Promises don't.

*) 我注意到 WinJS 有.done(..)我想要的方法,但 Native Promises 没有。

回答by Valentin Shergin

The whole world is waiting for the unhandledrejectionand rejectionhandledevents. As of March 2016, Chrome is now the first to support it.

整个世界都在等待unhandledrejectionrejectionhandled事件。截至 2016 年 3 月,Chrome 现在是第一个支持它的。

Example:

例子:

window.addEventListener('unhandledrejection', function(event) {
    console.error('Unhandled rejection (promise: ', event.promise, ', reason: ', event.reason, ').');
});

Specification: HTML Living Standard

规范:HTML 生活标准

Mozilla Developer: onrejectionhandled, onunhandledrejection

Mozilla 开发者: onrejectionhandled, onunhandledrejection

Chromium Issues: 495801, 393913

铬的问题:495801393913

回答by Benjamin Gruenbaum

Note that in Node the event is called unhandledRejection:

请注意,在 Node 中,该事件被称为unhandledRejection

process.on('unhandledRejection', function(err, promise) {
    console.error('Unhandled rejection (promise: ', promise, ', reason: ', err, ').');
});

On version 12+, node will terminate on these rejections.

在版本 12+ 上,节点将在这些拒绝时终止。

回答by Benjamin Gruenbaum

Some libraries have their own APIs for doing this. Some browsers will report unhandled rejections (sooner or later).

一些库有自己的 API 来执行此操作。一些浏览器会报告未处理的拒绝(迟早)。

Actually, doneprobably does notdo what you want. This is why it is not part of the spec. In any case, you still have to remember to call it.

事实上,done可能根本不会做你想做的。这就是为什么它不是规范的一部分。无论如何,你还是要记得调用它。

There is no reliable, cross-platform, cross-library way to do this.

没有可靠的、跨平台、跨库的方式来做到这一点。

回答by Aleksandr Oleynikov

uncaught librarycan help you to catch unhandled promise rejections.

uncaught 库可以帮助您捕获未处理的承诺拒绝。

And it handles uncaught errors as well.

它也处理未捕获的错误。

EDIT

编辑

<script type="text/javascript" src=".../uncaught/lib/index.js"></script>

<script type="text/javascript">
    uncaught.start();
    uncaught.addListener(function (error) {
        console.log('Uncaught error or rejection: ', error.message);
    });
</script>

Benefit of this approach is the only one interface, which allows you to handle both uncaught errors and unhandled promise rejections.

这种方法的好处是唯一的一个接口,它允许您处理未捕获的错误和未处理的承诺拒绝。