Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Managing globally errors with $resource and $http in AngularJS

I have been looking for a way of managing the http errors that I can have in my angular application. I have been capturing them separately, but I would like to manage them globally. I am using $resource and $http for my http calls. Are there any way of managing them globally?

like image 736
Jorge Guerola Avatar asked Oct 21 '22 15:10

Jorge Guerola


1 Answers

Try $http interceptor:

Quoted from the docs:

angular.module('app',[]).config(function($httpProvider){

    $httpProvider.interceptors.push(function($q, dependency1, dependency2) {
      return {
       'request': function(config) {
           return config;
        },

       'response': function(response) {
           // do something on success
           return response || $q.when(response);
        },
        'responseError': function(rejection) {
         // do something on error

           return $q.reject(rejection);
         }
      };
    });
});
like image 126
Khanh TO Avatar answered Oct 23 '22 08:10

Khanh TO