The log shows the error object: {"isTrusted": true} instead of the actual error data

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 get is as follows:

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

As you can see, the line number or error message is useful. What do I need to change to get the line number and error information?

+5
source share
2 answers

In this case, you need to know two points. Both points are independent of each other and must be fixed to solve your problem.

First

The error you encounter is a special type of error called Script Error

A “Script error” is what browsers send to an onerror when an error occurs from a JavaScript file filed from a different source (different domain, port or protocol). It hurts because even though an error occurs, you don’t know what the error is, it didn’t come from any code.

This is not a JavaScript error.

Browsers intentionally hide errors from script files from various sources for reasons of safety. To avoid unintentional leakage of script-sensitive information to the onerror return signal, which it does not control. For this reason, browsers only give window.onerror understanding of errors originating from the same domain. All we know is that the error happened - nothing more!

To fix this problem:

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

Second

When you try to compress any Error object, the result will not satisfy at all, because you will lose almost all the data.

The reason of that

JSON.stringify deals only with enumerated properties, but the Error object stores contextual data in unnamed properties.

To fix this problem

There are many solutions, but it can be straightforward.

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

This selects the properties you need and generates a string for you.

+7
source

"Script error" probably means that the problem is an attempt to execute a script from an external domain.

You do not have line number and error information because this is not on your page.

It is covered in some detail in the answers to this question .

+1
source

Source: https://habr.com/ru/post/1269310/


All Articles