日志显示错误对象:{" isTrusted":true}而不是实际的错误数据 [英] Log shows Error object: {"isTrusted":true} instead of actual error data

查看:3690
本文介绍了日志显示错误对象:{" isTrusted":true}而不是实际的错误数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

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

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.

错误你面临的是一种特殊类型的错误,称为 脚本错误

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


脚本错误是浏览器发送给 onerror
错误源自从不同来源
(不同的域,端口或协议)提供的JavaScript文件时,回调。这很痛苦,因为即使出现错误,即使是
,你也不知道错误是什么,
也不知道它来自哪个代码。

"Script error" is what browsers send to the onerror callback 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.

这不是JavaScript错误


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

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.onerror insight into errors originating from the same domain. All we know is that an error occurred – nothing else!

解决这个问题:

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

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

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

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

原因

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

JSON.stringify deals only with enumerable properties but Error object stores the contextual data in inenumerable properties.

解决此问题

有多种解决方案,但这可能是直截了当的

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.

这篇关于日志显示错误对象:{" isTrusted":true}而不是实际的错误数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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