使用 window.onerror 获取实际的 Javascript Error 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7099127/
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-24 00:50:53  来源:igfitidea点击:

Get the actual Javascript Error object with window.onerror

javascripterror-handlingonerror

提问by Julien Genestoux

Javascript has this great callback window.onerror. It's quite convenient to track any error. However, it calls with the error name, the file name and the line. It's certainly not as rich as getting the actual error object from a try...catchstatement. The actual error object contains a lot more data, so I am trying to get that. Unfortunately, try...catchstatement do not work fine when you start having async code.

Javascript 有这个很棒的回调window.onerror。跟踪任何错误非常方便。但是,它使用错误名称、文件名和行进行调用。它当然不如从try...catch语句中获取实际的错误对象那么丰富。实际的错误对象包含更多的数据,所以我试图得到它。不幸的是,try...catch当您开始使用异步代码时,语句无法正常工作。

Is there a way to combine and get the best of both worlds? I initially looked for a way to get the lasterror triggered within an onerrorblock, but it looks like JS doesn't store that.

有没有办法结合并获得两全其美?我最初寻找一种方法来在块内触发最后一个错误onerror,但看起来 JS 不存储它。

Any clue?

有什么线索吗?

采纳答案by Mrchief

If you're referring to stack trace of the error object, then AFAIK, this is not possible.

如果您指的是错误对象的堆栈跟踪,那么 AFAIK,这是不可能的。

Simple reason being that a stack trace is related to an execution context in which runtime exceptions (handled with try...catch...finally) were created or thrown (with new Error()or throw).

一个简单的原因是堆栈跟踪与运行时异常(用 try...catch...finally 处理)被创建或抛出(用new Error()throw)的执行上下文相关。

Whereas when window.onerroris invoked, it is called within a different context.

而 whenwindow.onerror被调用,它在不同的上下文中被调用。

You can get some mileage by inspecting window.event(not available on FF) in your onerrorhandler.

您可以通过window.event在您的onerror处理程序中检查(在 FF 上不可用)来获得一些里程数。

回答by Archaeron

this is now possible in some browsers. The specwas updated to include the actual error with stacktrace as the 5th parameter.

这现在在某些浏览器中是可能的。该规范已更新为包括与堆栈跟踪作为第五个参数的实际错误。

the problem is that not every browser supports this yet, so you could do something like this:

问题是并不是每个浏览器都支持这个,所以你可以做这样的事情:

window.onerror = function(message, filename, lineno, colno, error)
{
    if(error != null)
    {
        //handle the error with stacktrace in error.stack
    }
    else
    {
        //sadly only 'message', 'filename' and 'lineno' work here
    }
};

回答by Fizer Khan

Modern browsers fully support the HTML 5 draft spec for ErrorEventand window.onerror. In both of these browsers you can either use window.onerror, or (amazingly!) bind to the 'error' event properly:

现代浏览器完全支持ErrorEventwindow.onerror. 在这两种浏览器中,您都可以使用 window.onerror,或者(非常棒!)正确绑定到 'error' 事件:

// Only Chrome & Opera pass the error object.
window.onerror = function (message, file, line, col, error) {
    console.log(message, "from", error.stack);
};
// Only Chrome & Opera have an error attribute on the event.
window.addEventListener("error", function (e) {
    console.log(e.error.message, "from", e.error.stack);
});