16

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.

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. Commented Apr 27, 2015 at 0:14
  • can you share complete code of error-response Commented Apr 27, 2015 at 4:59
  • 1
    @DaveAlperovich could you provide example with different interceptors in my case? 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? Commented Apr 27, 2015 at 20:41
  • @Claies backend is developed in such strange way... Commented Apr 28, 2015 at 11:48

5 Answers 5

3
+50

In 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) {

}
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

1

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

Comments

1

so... i have done it so:

    if(rejection.config.url.indexOf('?page=') > -1) {
      skipException = true;
    }

Comments

-1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.