How to catch HTTP connection errors in AngularJS?

I have a set of $resource . There are times when the API server that I use to get their timeout or its domain name cannot be resolved. These are expected cases.

How can I catch these errors in AngularJS? I tried setting up the $http interceptor, but it is not very verbose.

 $httpProvider.interceptors.push(function($q, $rootScope) { return { responseError: function(rejection) { console.log(rejection); if(rejection.status <= 0) { $rootScope.$emit('httpConnection:error', rejection); } return $q.reject(rejection); } }; }); 

Console outputs

 GET http://my.api/api/endpoint.json net::ERR_NAME_NOT_RESOLVED Object {data: null, status: -1, config: Object, statusText: ""} 

Pay attention to status: -1 and statusText: "" .

I would like to have error indication ERR_NAME_NOT_RESOLVED . Is there a way to get this error in the responseError handler parameter?

+5
source share

All Articles