In my app I use an interceptor to catch all http response errors, like:
var response = function(response) {
if(response.config.url.indexOf('?page=') > -1) {
skipException = true;
}
return response;
}
var responseError = function(rejection) {
if (rejection.status === 401 || rejection.status === 403) {
/**/
}
else if (rejection.status >= 500 || rejection.status === 0) {
/**/
}
else if (rejection.status === 404 && !skipException) {
/**/
}
else if (rejection.status === 404 && skipException) {
/**/
}
else{
/**/
}
return $q.reject(rejection);
};
And when I go to my controller (when my getArticles
method returns some data, not 404 - when articles array is empty) all is OK: 404 with skipException == true
is caught.
But when my articles array is empty the server returns a 404 and when I enter this controller I cannot get response.config.url
-- no response is caught, but why? I thought that interceptor would catch all of the responses.
$timeout(function() {
$scope.getArticles();
}, 100);
and $scope.getArticles
has such code:
getDataService.getArticles($scope.pageNum).then(function(response) {
/**/
});
service:
var getEventsByScrollService = function(num) {
var deferred = $q.defer();
$http.get(***, {
})
.success(function(response) {
deferred.resolve(response);
}).error(function(err, status) {
if (status === 404){
deferred.resolve([]);
}
else{
deferred.reject(err);
}
});
return deferred.promise;
};
How can I conditionally catch 404's depending on the URL? Because this:
if(response.config.url.indexOf('?page=') > -1) {
Doesn't always work.
In my app I use an interceptor to catch all http response errors, like:
var response = function(response) {
if(response.config.url.indexOf('?page=') > -1) {
skipException = true;
}
return response;
}
var responseError = function(rejection) {
if (rejection.status === 401 || rejection.status === 403) {
/**/
}
else if (rejection.status >= 500 || rejection.status === 0) {
/**/
}
else if (rejection.status === 404 && !skipException) {
/**/
}
else if (rejection.status === 404 && skipException) {
/**/
}
else{
/**/
}
return $q.reject(rejection);
};
And when I go to my controller (when my getArticles
method returns some data, not 404 - when articles array is empty) all is OK: 404 with skipException == true
is caught.
But when my articles array is empty the server returns a 404 and when I enter this controller I cannot get response.config.url
-- no response is caught, but why? I thought that interceptor would catch all of the responses.
$timeout(function() {
$scope.getArticles();
}, 100);
and $scope.getArticles
has such code:
getDataService.getArticles($scope.pageNum).then(function(response) {
/**/
});
service:
var getEventsByScrollService = function(num) {
var deferred = $q.defer();
$http.get(***, {
})
.success(function(response) {
deferred.resolve(response);
}).error(function(err, status) {
if (status === 404){
deferred.resolve([]);
}
else{
deferred.reject(err);
}
});
return deferred.promise;
};
How can I conditionally catch 404's depending on the URL? Because this:
if(response.config.url.indexOf('?page=') > -1) {
Doesn't always work.
Share Improve this question edited Apr 27, 2015 at 20:36 Tom 7,7401 gold badge26 silver badges49 bronze badges asked Mar 13, 2015 at 8:26 byCoderbyCoder 9,18427 gold badges119 silver badges259 bronze badges 6- 2 Checking logic by URL sounds wrong. Seems you should have different Services and or Service methods for different interceptor behaviors, The Service Methods should call different interceptors which call different Error Resolvers. One of the Error Resolvers should handle 404's and another should ignores 404's. The service method should decide this. Coupling logic with URL's sounds like a violation of SoC. – Dave Alperovich Commented Apr 27, 2015 at 0:14
- can you share complete code of error-response – harishr Commented Apr 27, 2015 at 4:59
- 1 @DaveAlperovich could you provide example with different interceptors in my case? – byCoder Commented Apr 27, 2015 at 7:20
- why would your server be returning 404 when the article array is empty? wouldn't that be a response 200 with the empty array as a return? – Claies Commented Apr 27, 2015 at 20:41
- @Claies backend is developed in such strange way... – byCoder Commented Apr 28, 2015 at 11:48
5 Answers
Reset to default 3 +50In an effort to be more maintainable and extendable to any $http service call one could do this:
// Service call
$http.get({url:'/?page=', ignoreErrors: true})
// Interceptor
if(rejection.status === 404 && !rejection.config.ignoreErrors) {
}
you can check out Restangular, might be useful for your purposes. It has good interceptor methods built in. Whether it's really good for you will depend on if you're using a RESTful API or not. https://github.com/mgonto/restangular
In the "response" you shoud check response url, not response.config.url.. something like this:
var response = function(response) {
if(response.url.indexOf('?page=') > -1) {
skipException = true;
}
return response;
}
at the response level you don't have the config
so... i have done it so:
if(rejection.config.url.indexOf('?page=') > -1) {
skipException = true;
}
From the documentation:
A response status code between 200 and 299 is considered a success status and will result in the success callback being called.
Since the server returns a 404, the responseError
function gets called. Unfortunately, the config parameter is not available so you can't do any conditional logic based on the request url in there.