gpt4 book ai didi

javascript - 日志显示错误对象 : {"isTrusted":true} instead of actual error data

转载 作者:行者123 更新时间:2023-12-03 03:10:53 26 4
gpt4 key购买 nike

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

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);

问题是我收到的内容如下:

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

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

最佳答案

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

第一

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

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

这不是 JavaScript 错误

Browsers intentionally hide errorsoriginating from script files from different origins for securityreasons. It’s to avoid a script unintentionally leaking potentiallysensitive information to an onerror callback that it doesn’t control.For this reason, browsers only give window.onerror insight into errorsoriginating from the same domain. All we know is that an erroroccurred – nothing else!

要解决此问题:

要修复并获取正常的错误对象,检查此 blog post

第二个

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

原因

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

解决此问题

解决方案有很多,但这个可能很简单

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

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

关于javascript - 日志显示错误对象 : {"isTrusted":true} instead of actual error data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44815172/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com