I have a "cancellable" angularJs $http call like this:
var defer = $q.defer()
$http.post("http://example.com/someUrl", {some: "data"}, {timeout: defer.promise})
And I cancel that ajax request using defer.resolve() because some logic requires it.
Some where else in my code, I have an iterceptor like this:
angular.module("services.interceptor", arguments).config(function($httpProvider) {
$httpProvider.interceptors.push(function($q) {
return {
responseError: function(rejection) {
if(rejection.status == 0) {
alert("check your connection");
}
return $q.reject(rejection);
}
};
});
});
});
Problem:
If there is an Internet connection problem, ajax fails with status 0 and interceptor catches it. If ajax is cancelled by the timeout promise, than status is also 0 and interceptor catches it.
I can't find out if it is cancelled or got error in responseError handler.
My naive approach is to check if timeout is defined like this:
responseError: function(rejection) {
if(rejection.status == 0 && !rejection.config.timeout) {
alert("check your connection");
}
return $q.reject(rejection);
}
It only guarantees that, there is a timeout condition on that request, not it failed because of it. But it is better than nothing.
Are there a really working way of determining if ajax is failed or cancelled?
I'm using AngularJs 1.1.5