This may seem like a strange request. I was wondering if there is a way using a $http interceptor to catch the first URL that has a response status of 500, then stop all sub
You could create a service and interceptor to deal with this,
.factory('ServerErrorService', function() {
var _hasError = false
return {
get hasError() {
return _hasError;
},
set hasError(value) {
_hasError = value;
// you could broadcast an event on $rootScope to notify another service that it has to deal with the error
},
clear: clear
}
function clear() {
_hasError = false;
}
})
.factory('ServerErrorInterceptor', function ($q, ServerErrorService) {
var interceptor = {
request: function(config) {
if(ServerErrorService.hasError) {
var q = $q.defer();
q.reject('prevented request');
return q.promise;
}
return config;
},
responseError: function(response) {
if(response.status === 500) {
ServerErrorService.hasError = true;
}
return $q.reject(response);
}
}
return interceptor;
})
If you wanted to allow requests to be made again then you just need to call ServerErrorService.clear()
OR
You could use a modified version of this answer. Although i'm not sure how - if you wanted to - you'd cancel this action and allow subsequent requests.
https://stackoverflow.com/a/25475121/1798234
.factory('ServerErrorInterceptor', function ($q) {
var canceller = $q.defer();
var interceptor = {
request: function(config) {
config.timeout = canceller.promise;
return config;
},
responseError: function(response) {
if(response.status === 500) {
canceller.resolve('server error');
}
return $q.reject(response);
}
}
return interceptor;
})
One thing to remember for both of these solutions, if you have any other interceptors after this that have a responseError
method defined or any handlers set up with .catch
to process the $http promise, they will receive a response object with {data:null, status:0}