es6承诺吞下类型错误 [英] es6 promises swallow type errors

查看:61
本文介绍了es6承诺吞下类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望浏览器在发生类型错误时显示错误消息。

错误,例如无法读取未定义属性未定义引用 >。

I want the browser to show an error message when a type error occurs.
errors like can not read property something of undefined or undefined reference.

new Promise(function(resolve,reject){
    // do stuff ...
    reject('something logical is wrong');
}).catch(e => console.error(e));

new Promise(function(resolve,reject){
    // do stuff, and a syntax error :/
    var a = { };
    a.something.otherthing = 1; /* we have an error here */
    // ... 
}).catch(e => console.error(e));

在第一个示例中,错误是一个合理的错误,捕获是可以的 catch(..)块中。

但是在第二个示例中,这是一个明显的开发错误,在开发新内容时总是发生。我不想抓住它,我希望浏览器向我显示该错误,就像控制台中的其他错误一样。
我希望能够打开 chrome异常暂停并查看其他变量的状态。我想在控制台中查看堆栈跟踪。

我希望它表现为 normal 错误。

In the first example the error is a logical one, and its fine to catch it in the catch(..) block.
But in the second example it is a clear development error, which happens all the time while developing new stuff. I don't want to catch it, i want the browser to show me the error like other errors in the console. I want to be able to turn on chrome pause on exceptions and see the state of other variables. I want to see the stack trace in console.
I want it to act like a normal error.

任何

推荐答案

与同步代码中的异常不同,同步代码中的异常在代码返回到 idle ,浏览器通常不知道Promise链的逻辑端,在这里,可以将异步错误视为未捕获。链毕竟是动态组装的,因此最好在链的逻辑端以最终的 .catch 终止,即异步等效于空闲。

Unlike exceptions in synchronous code, which become uncaught as soon as code returns to idle, a browser generally doesn't know the logical end of a promise-chain, which is where an asynchronous error could be considered uncaught. Chains are dynamically assembled after all, and therefore better be terminated with a final .catch at the logical end of the chain i.e. the asynchronous equivalent of idle.

具有最终的 .catch(e => console.error(e))对我来说似乎很合理,但您说得对浏览器倾向于以与未捕获的异常不同的方式显示这些错误。如果希望它们显示相同,可以使用以下技巧:

Having a final .catch(e => console.error(e)) seems very reasonable to me, but you're right that browsers tend to display these errors differently from uncaught exceptions. If you want them to appear the same, you can use this trick:

.catch(e => setTimeout(() => { throw e; }))

这将抛出 e ,包含原始堆栈跟踪和行号,位于下一个循环中,位于promise链之外,没有任何东西可以捕获它,并且将报告为未捕获。我们使用 setTimeout 来克服 .catch 的默认行为,该行为是在链中捕获任何异常,以防您打算

This will throw e, containing the original stack trace and line number, on the very next cycle, and outside of the promise chain, where nothing will catch it and it will be reported as uncaught. We use setTimeout to overcome the default behavior of .catch which is to capture any exceptions in-chain in case you intend to keep on chaining.

我希望您看到逻辑错误与其他错误之间的任何区别都是不相关的。导致链条尾部的任何错误都会对链条造成致命影响,即未被捕获(当然,您可以将最终捕获中的其他错误与逻辑进行分类,并根据需要进行不同显示。 )

With this, I hope you see that any differentiation between "logical" and other errors is irrelevant. Any error that makes it to the tail of the chain was fatal to the chain i.e. uncaught (though of course you can triage "logical" from other errors in the final catch and display them differently if you choose.)

这篇关于es6承诺吞下类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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