Detect all JS errors using JS

I know this was probably asked earlier, but I cannot find where:

I know that you can detect JS errors using extensions in the materials, but is there a way to detect ALL errors with JavaScript and display a warning whenever there is one?

+8
javascript error-handling
source share
1 answer

In the browser, define the window.onerror function. In a node attached to an uncaughtException event using process.on() .

This should be ONLY if you need to catch all the errors, for example, in the spec slider or console.log / debugging implementation. Otherwise, you will find yourself in a world of pain, trying to track strange behavior. Like some of them, in normal daily code, the try / catch is the right and best way to handle errors / exceptions.

For reference in the first case, see this (about window.error in browsers) and this (about uncaughtException in node) . Examples:

Browser

 window.onerror = function(error) { // do something clever here alert(error); // do NOT do this for real! }; 

Node.js

 process.on('uncaughtException', function(error) { // do something clever here alert(error); // do NOT do this for real! }); 
+16
source share

All Articles