question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JSDOM error handling

See original GitHub issue

Is there any way to make JSDOM forward script errors to Node.js?

I assumed the following would log undefined has no method 'toString' but the errors are null:

jsdom.env(
  '<html><script>nonExistentVariable.toString()</script></html>'
, []
, function (errors, window) {
    console.log(errors)
  }
)

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
domeniccommented, Jan 22, 2014

You are sending the messages to the window’s console, but that is different from the Node.js console. Try this instead:

var document = jsdom('<!DOCTYPE html> <script>window.errors = [];' +
  'window.onerror = function (message) { window.errors.push(message); };' +
  'nonExistentVariable.toString();</script>');

console.log(document.parentWindow.errors);
0reactions
SystemParadoxcommented, Apr 5, 2016

In case this helps anyone else, in 3.1.1 I had to add the following to forward errors from event scripts (otherwise they go silently unnoticed):

document.raise = function (type, message, data) {
    if (data.error) {
        throw data.error;
    } else {
        console.error('ERROR', message);
    }
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I catch errors thrown by my jsdom.jqueryify callback?
js is the stack trace of an error that occurs on the last line (line number 9). The error that occurs on line...
Read more >
jsdom - npm
Script execution errors that are not handled by a window onerror event handler that returns true or calls event.preventDefault(); Not- ...
Read more >
jsdom - Mediumish
Note that as of our 7.0.0 release, jsdom requires Node.js 4 or newer (why?). ... Script execution errors that are not handled by...
Read more >
How to use the jsdom.VirtualConsole function in jsdom - Snyk
function mockify(html = '') { // Start of message that we want to suppress. let msg = 'Error: Not implemented: HTMLCanvasElement.prototype.
Read more >
An Alternative Approach to JavaScript Error Handling
The cleanest way to handle errors is to use exceptions¹. Of course, a try-catch block is far more convenient than returning errors.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found