08 April, 2013

Global Error Handling through javascript

In java script we can handle all  uncaught errors on a single place which is handy to manage exception handling in application.

There are try catch block to handle block level exception but there are so many which left and thrown run time. Following code can do the trick for this.

1:  window.addEventListener("error", function (evt) {  
2:    log.error(evt.message   
3:             + "\n File: "   
4:             + parseURL(evt.filename).attr["file"]   
5:             + "\n Line No: "  
6:             + evt.lineno);  
7:    console.log(evt);  
8:  });  

parseURL() is UFD used to extract filename from path.

log.error() is a UFD used to log error in file.

these two function can be written according to your need.

0 comments: