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.


  window.addEventListener("error", function (evt) {  
    log.error(evt.message   
             + "\n File: "   
             + parseURL(evt.filename).attr["file"]   
             + "\n Line No: "  
             + evt.lineno);  
    console.log(evt);  
  });  

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.

No comments:

Post a Comment

Suggestions are invited from readers