Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic feedback on JavaScript error

Is there a way to get an automatic feedback if an error (even syntax errors) occurs when running a JavaScript on the client side?

I was thinking of something like this:

<script src="debugger.js"></script>
<script>
    // some script with an error in it
</script>

And every time the debugger notices an error it sends a feedback to the server.

like image 828
Gumbo Avatar asked Feb 13 '09 17:02

Gumbo


People also ask

Why does a JavaScript error keep popping up?

Grammatical mistakes, such as missing parentheses or unmatched brackets, are the major causes of syntax errors in JavaScript. For example, when you must use conditional statements to address multiple conditions, you may miss providing the parentheses as required, leading to syntax flaws.

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 are the different types of errors in JavaScript?

There are three main types of errors that can occur while compiling a JavaScript program: syntax errors, runtime errors, and logical errors.


2 Answers

EDIT: I misunderstood your question initially, this should work:

Also note, this needs to go BEFORE any javascript that might cause errors.

window.onerror = function(msg,u,l)
{
    txt ="Error: " + msg + "\n";
    txt+="URL: " + u + "\n";
    txt+="Line: " + l + "\n\n";
    //Insert AJAX call that passes data (txt) to server-side script
    return true;
};

As for syntax errors, you're out of luck. Javascript simply dies when there's a syntax error. There's no way to handle it at all.

like image 83
Andrew Ensley Avatar answered Sep 28 '22 20:09

Andrew Ensley


One technique is to use Ajax for Javascript error logging

Every javascript error can be trapped in the window.onerror event. We can return true or false so we can choose if the user shall see the normal javascript error dialog. This script will, in the very unlikely event of a javascript error, gather information about the error and send a httprequest to a page which will log the javascript error to the database.

like image 45
Gulzar Nazim Avatar answered Sep 28 '22 19:09

Gulzar Nazim