15

Below is my interceptor which handles global errors. But I want to bypass some http requests. Any suggestions ?

 var interceptor = ['$rootScope', '$q',function (scope, $q) {
        function success(response) {
            return response;
        }
        function error(response) {
            var status = response.status;
            if (status == 401) {
                window.location = "./index.html#/404";
                return;
            }
            if (status == 0) {
                window.location = "./index.html#/nointernet";
            }
            return $q.reject(response);
        }
        return function (promise) {
            return promise.then(success, error);
        }
    }];
    $httpProvider.responseInterceptors.push(interceptor);

3 Answers 3

33

I was able to implement this functionality simply by adding a property to the config object of the $http request. ie. ignore401. Then, in my interceptor, in the response error handler, check for the property on the config object, and if it is present, do not forward to login or whatever else you do on a 401 response.

First, the interceptor:

$provide.factory('authorization', function() {
    return {
        ...

        responseError: (rejection) => {
            if (rejection.status === 401 && !rejection.config.ignore401) {
                // redirect to login
            }

            return $q.reject(rejection);
        }
    };
});

Then, for any request that I want to bypass the 401 error handler:

$http({
    method: 'GET',
    url: '/example/request/to/ignore/401error',
    ignore401: true
});

Hope that helps.

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

2 Comments

but there is no certainty that adding a "custom" property on the config object on request will still be available in the response, is there?
@cipak - The documentation suggests that the config object sent is returned on the response object from the promise. config – {Object} – The configuration object that was used to generate the request. from: code.angularjs.org/snapshot/docs/api/ng/service/$http
4

I'm solving this as follows:

$httpProvider.interceptors.push(function($rootScope) {
    return {
      request: function(config) {
        var hideUrl = "benefit/getall"; // don't show the loading indicator for this request
        var hide = (config.url.indexOf(hideUrl)); // is this isn't -1, it means we should hide the request from the loading indicator
        if(hide == -1)
        {
          $rootScope.$broadcast('loading:show')

        }
        return config
      },
      response: function(response) {
        $rootScope.$broadcast('loading:hide')
        return response
      }
    }
  });

Comments

0

Doesn't the response object contain an "options" property where you can inspect what URL was used in the request?

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.