2

I need to log all the client-side errors and send error stack trace to server-side. What are the best practices to log the client-side errors and how should I go about it ?

Vaibhav Pachauri
  • 2,671
  • 2
  • 19
  • 32

1 Answers1

1

I assume you are mainly concerned about $http errors (non 2xx).

One way of doing that would be to configure global interceptor and perform some action in the 'responseError' handler:-

// register the interceptor as a service
$provide.factory('myHttpInterceptor', function($q, dependency1, dependency2) {
    return {

        'responseError': function(rejection) {
            // DO YOUR WAY OF HANDLING here.

            return $q.reject(rejection);
        }
    };
});

$httpProvider.interceptors.push('myHttpInterceptor');

Your reponseErrorHandler can take care of non 2xx http statuses (5xx,4xx). You might want to treat them differently as per your need.

You can trigger another ajax request here to your RESTful logging framework.

Kumar Sambhav
  • 7,503
  • 15
  • 63
  • 86
  • Yes, that is a nice way of doing it. But I am not only looking for the $http errors but also the errors that have occurred in my javascript code which is being printed in the console of the browsers. Whatever be the error, needs to be logged to the server-side. – Vaibhav Pachauri Jan 21 '16 at 11:46
  • try http://stackoverflow.com/questions/5328154/catch-all-javascript-errors-and-send-them-to-server – Kumar Sambhav Jan 21 '16 at 11:50