Utilizing angular-spinner to display a loading spinner while intercepting all http requests made by my application.
Here is the relevant code snippet:-
//spinner configuration START
myApp.factory('spinnerInterceptor', ['usSpinnerService', function(usSpinnerService) {
return {
request: function(config) {
usSpinnerService.spin('spinner-1');
return config;
},
response:function(config){
usSpinnerService.stop('spinner-1');
return config;
},
responseError:function(config){
usSpinnerService.stop('spinner-1');
return config;
}
};
}]);
myApp.config(['$httpProvider', function($httpProvider) {
$httpProvider.interceptors.push('spinnerInterceptor');
}]);
//spinner configuration END
Aside from managing the spinner, the interceptor just returns the config object.
Issue:-
Despite receiving a 404 error message from one of my POST RESTful endpoints, the success handler within the $http block is still executed. Why is this happening?
$http({
method : 'POST',
url : url,
params :paramsJson,
data : _data
}).success(function(data,status) {
// THIS BLOCK GETS EXECUTED AFTER THE SPINNER INTERCEPTOR EVEN WITH STATUS 404 !!!!
}).error(function(data,status) {
// I EXPECT THIS BLOCK TO RUN IN CASE OF A 404 OR ANY NON 2XX RESPONSE
});
The spinnerInterceptor is being called before the success/error handlers of the $http block, affecting the error handling of the promise returned.
When the spinner interceptor is removed, everything functions as intended.
Any assistance in resolving this is greatly appreciated.