Robust Javascript Exception Handling

I am developing a DHTML / Javascript application that relies on some additional features (DOM manipulation, AJAX, Flash communication, etc.). I am very concerned about functionality - if there are problems, even after deploying the application, I want to make sure that I know why and how to fix them - and I also want the user to be able to continue using the application, possibly with reduced functionality, if there was an exception serious.

I am currently creating a system for processing logs and exceptions in which functions can generate logs, and if an exception is caught, all logs are sent to me by email. The system works well, but I would like to make it more reliable. I am looking for offers.

One of my ideas is to wrap the body of each javascript function in a try / catch block and, having caught the exception, write down the name of the function and then throw the error into the global handler. But for this, there is a lot of code to track the function in which the exception occurred.

Any ideas to make it easier to find and throw runtime exceptions?

+6
javascript exception exception-handling
source share
1 answer

Instead of dealing with adding N try / catch blocks to N functions, it would be easier to use the window.onerror event.

The JavaScript Kit has a series of examples that you could use. Especially 3rd :

 window.onerror = function (msg, url, line) { alert('Error message: ' + msg + '\nURL: ' + url + '\nLine Number: ' + line); return true; } 

If you prefer stack tracing, you can check Eric Wendelin's (or Luke Smith Update ). This is one of the few I know about trying to work with a cross browser.

+4
source share

All Articles