1

When I am developing a client-side application, I wish there were a way to show Javascript runtime errors on the screen, so I don't have to check the console all the time to see if there was an error.

The only way I could imagine this working, would be some sort of error listener, something like this:

window.on('error',function(err){
 $(document).html(JSON.stringify(err));
});

is there anything like this out there? When an error is thrown in front-end JS, can we create a handler for all those errors that bubble up to the global scope?

There is this, https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onerror, but it says that many errors do not get caught/trapped by that handler.

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817

1 Answers1

1
window.onerror = function(message, url, lineNumber) {  
   $(document).html(JSON.stringify(err));
  return true;
};  

You can probably try this out.

Keep in mind that returning true will prevent the firing of the default handler, and returning false will let the default handler run.

Source: https://stackoverflow.com/a/5328206/1500341

Community
  • 1
  • 1