2

I want to implement the global error handling for Ajax calls in my angular Js Application . I think $httpprovider works for $http requests only. Kindly guide me in achieving this. How can i write the interceptor for capturing the failure of Ajax calls. My main goal is to capture the failure of Ajax call into google Analytics.

Mohan
  • 21
  • 2

2 Answers2

0

Write ajax call inside javascripts try-catch

http://www.w3schools.com/js/js_errors.asp
akhil viswam
  • 496
  • 9
  • 24
  • I have multiple REST APIs to consume. So i want to do that Globally and don't want write the capturing function inside every catch block – Mohan May 05 '16 at 08:50
0

You can try to listen to window.onerror event but it will catch all js errors, so you need to filter them.

window.onerror = function(msg, url, line, col, error) {
   // Note that col & error are new to the HTML 5 spec and may not be 
   // supported in every browser.  It worked for me in Chrome.
   var extra = !col ? '' : '\ncolumn: ' + col;
   extra += !error ? '' : '\nerror: ' + error;

   // You can view the information in an alert to see things working like this:
   alert("Error: " + msg + "\nurl: " + url + "\nline: " + line + extra);

   // TODO: Report this error via ajax so you can keep track
   //       of what pages have JS issues

   var suppressErrorAlert = true;
   // If you return true, then error alerts (like in older versions of 
   // Internet Explorer) will be suppressed.
   return suppressErrorAlert;
};

You can read more about it in this question.

Community
  • 1
  • 1
Dmitriy Nevzorov
  • 6,042
  • 1
  • 20
  • 28