Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to suppress all JavaScript runtime errors?

How can I suppress all JavaScript runtime error popups, from the programmers side?

like image 573
Skip Avatar asked Aug 19 '11 10:08

Skip


People also ask

What causes runtime errors in JavaScript?

Runtime errors occur when the interpreter comes along some code that it cannot understand. Both loading and syntax errors are simple but can result in unexpected halting of script.

How do you handle JavaScript errors?

JavaScript provides error-handling mechanism to catch runtime errors using try-catch-finally block, similar to other languages like Java or C#. try: wrap suspicious code that may throw an error in try block. catch: write code to do something in catch block when an error occurs.

What is the most common error in JavaScript?

TypeError is one of the most common errors in JavaScript apps. This error is created when some value doesn't turn out to be of a particular expected type. Some of the common cases when it occurs are: Invoking objects that are not methods.

What is compilation error in JavaScript?

Compilation error refers to a state when a compiler fails to compile a piece of computer program source code, either due to errors in the code, or, more unusually, due to errors in the compiler itself. A compilation error message often helps programmers debugging the source code.


2 Answers

To suppress all JavaScript errors there seems to be a window.onerror event.
If it is replaced as early as possible(e.g. right after the head) by a function which returns true - there will be no error popups, which is quite useful after debugging is done.

<head>
<script type="text/javascript">
    window.onerror = function(message, url, lineNumber) {  
        // code to execute on an error  
        return true; // prevents browser error messages  
    };
</script> 
...

Error Logging is possible too, as explained here

like image 107
Skip Avatar answered Oct 07 '22 23:10

Skip


I made a little script for suppressing an error like "@" does in PHP.

Here is an example.

var at = function( test ) {
    try {
        if(typeof test === "function") return test();            
        else return test || null;        
    } catch (e) {
        return null;
    }
};
like image 21
Leonardo Ciaccio Avatar answered Oct 08 '22 01:10

Leonardo Ciaccio