62

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?

1
  • 1
    Which one is your problem : #1) serialization of error to JSON ? Or #2) How to get message and lineno in the first place ? For #1, have you seen this Q&A? In particular this answer and suggestion to use replacer parameter for JSON.stringify(): JSON.stringify(e, Object.getOwnPropertyNames(e)). Commented Jul 1, 2017 at 13:38

2 Answers 2

87
+50

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” 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.

This isn’t a JavaScript bug

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 this problem:

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

Second

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

The reason for that

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

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.

Sign up to request clarification or add additional context in comments.

4 Comments

the "To fix and get a normal error object" link is now broken... this is why it's a pain when solutions consist of links on other websites. Anyway I managed to find a copy in archive.org
Instead using JSON.stringify(err); use console.error("Error: ", err). Note err is passed as argument, so whole error would be printed out. This way you will not need to guess field names.
@hybaken If you only need to log then use console. But for example, if you need to save the error to display later (Into a file or over HTTP), you need to serialize it somehow, and JSON.stringify is one way of doing this.
An easy way I found to get the actual error is to use the debugger. If I expand the error in the console and navigate to the frame at the top of the stack, there's an error message (it was named o in my minified code) that's readily viewable but not displayed with the printed error. Or at least that's the case for errors originating in user scripts.
2

"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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.