Javascript 日志显示错误对象:{"isTrusted":true} 而不是实际的错误数据

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

Log shows Error object: {"isTrusted":true} instead of actual error data

javascriptexception-handling

提问by frenchie

I have an event handler that looks like this:

我有一个如下所示的事件处理程序:

window.addEventListener('error', function (e) {

    SendLogErrorToServer('Error: ' + e.message +
                         'Error object: ' + JSON.stringify(e) +
                         'Script: ' + e.filename +
                         'Line: ' + e.lineno +
                         'Col: ' + e.colno +
                         'Nav: ' + window.navigator.userAgent));

}, false);

The problem is that what I receive looks like this:

问题是我收到的东西是这样的:

Error: Script error.Error object: {"isTrusted":true} Script: Line: 0 Col: 0 Nav: Mozilla/5.0

As you can see, no line number or error message that's useful. What do I need to change to get the line number and error details?

如您所见,没有有用的行号或错误消息。我需要更改什么才能获取行号和错误详细信息?

回答by Mouneer

There are two points you need to be aware of in this case. Both points are independent of each other and should be fixed to solve your problem.

在这种情况下,您需要注意两点。这两个点彼此独立,应该修复以解决您的问题。

First

第一的

The error you're facing is a special type of errors called Script Error

您面临的错误是一种特殊类型的错误,称为 Script Error

“Script error” is what browsers send to the onerrorcallback when an error originates from a JavaScript file served from a different origin (different domain, port, or protocol). It's painful because even though there's an error occurring, you don't know what the error is, nor from which code it's originating.

“脚本错误”是onerror当错误源自不同来源(不同域、端口或协议)提供的 JavaScript 文件时,浏览器发送给回调的内容。这很痛苦,因为即使发生了错误,您也不知道错误是什么,也不知道错误来自哪个代码。

This isn't a JavaScript bug

这不是 JavaScript 错误

Browsers intentionally hide errors originating from script files from different origins for security reasons. It's to avoid a script unintentionally leaking potentially sensitive information to an onerror callback that it doesn't control. For this reason, browsers only give window.onerrorinsight into errors originating from the same domain. All we know is that an error occurred – nothing else!

出于安全原因,浏览器有意隐藏来自不同来源的脚本文件的错误。这是为了避免脚本无意中将潜在的敏感信息泄露给它无法控制的 onerror 回调。出于这个原因,浏览器只能window.onerror洞察源自同一域的错误。我们只知道发生了错误——没有别的!

To fix this problem:

要解决此问题:

To fix and get a normal error object, Check this blog post

要修复并获取正常的错误对象,请查看此博客文章

Second

第二

When you try to stringify any ErrorObject, the result will not be satisfying at all because you will lose almost all data.

当您尝试对任何Error对象进行字符串化时,结果根本不会令人满意,因为您将丢失几乎所有数据。

The reason for that

这样做的原因

JSON.stringifydeals only with enumerable properties but Errorobject stores the contextual data in inenumerable properties.

JSON.stringify只处理可枚举的属性,但Error对象将上下文数据存储在不可枚举的属性中。

To fix this problem

为了解决这个问题

There are number of solutions but this one could be straight forward

有很多解决方案,但这个可能是直接的

JSON.stringify(err, ["message", "arguments", "type", "name"])

This picks the properties you want and generate the string for you.

这会选择您想要的属性并为您生成字符串。

回答by user7363719

"Script error" probably means that the issue is with an attempt at executing a script from an external domain.

“脚本错误”可能意味着问题在于尝试从外部域执行脚本。

You don't have information on the line number and error details because it's not on your page.

您没有关于行号和错误详细信息的信息,因为它不在您的页面上。

It's covered pretty extensively in the answers to this question.

它在这个问题的答案中得到了广泛的介绍。