Catch all JavaScript errors and send them to server

Olivier Girardot picture Olivier Girardot · Mar 16, 2011 · Viewed 80.6k times · Source

I wondered if anyone had experience in handling JavaScript errors globally and send them from the client browser to a server.

I think my point is quite clear, I want to know every exception, error, compilation error, etc. that happens on the client side and send them to the server to report them.

I’m mainly using MooTools and head.js (for the JS side) and Django for the server side.

Answer

Mike Lewis picture Mike Lewis · Mar 16, 2011

I'd check out window.onerror

Example:

window.onerror = function(message, url, lineNumber) {  
  //save error and send to server for example.
  return true;
};  

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