title: $http Interceptor is not invoking on response error
body: <p>This is my Upload-Update page and when response is 401 its not automatically invoking <code>app.init</code> <a href="https://docs.angularjs.org/api/ng/service/$http#interceptors" rel="nofollow noreferrer">http interceptor</a> code. Instead <code>$scope.close()</code> i tried many thing i know i have to change <code>$scope.close()</code> to invoke interceptor but need to change i m not getting for other scenario its automatically interceptor :</p>
  
  <pre><code>angular.module('Myapp')
  .controller('CatalogueUploadDialogCtrl', [ '$scope', '$window', '$location', '$log', 'dialog', 'UiService', 'I18nService', 'ApiErrorHandlerService', 'EntitySaveService',
      function($scope, $window, $location, $log, dialog, uiService, i18n, apiErrorHandler, entitySaveService) {
          $scope.close = function() {
              dialog.close(); 
          }
  
          $scope.isOpenDisabled = function() {
              return ($scope.formController &amp;&amp; $scope.formController.inProgress())
                  || !($scope.model &amp;&amp; $scope.model.file &amp;&amp; $scope.model.file.name);
          }
  
          $scope.open = function($event) {
              if(!$scope.isOpenDisabled()) {
                  entitySaveService.flush()
                      .then(function() {
                          $scope.formController.submit()
                              .then(
                                  function success(classLibraryHandle) {
                                      $scope.classLibraryHandle = classLibraryHandle;
                                      $scope.close();
                                  },
                                  function (response) {
                                      // I've found no good way to reset the formController;
                                      // closing the dialog on error.
                                      $scope.close();
                                  });
  
                          });
              }
          }
      }
  ])
  </code></pre>
  
  <p>----------------Http interceptor code in app.init.js page-------------</p>
  
  <pre><code>.config([ '$httpProvider', function($httpProvider, i18n) {
      var handling401;
  
      // Add header to allow server side to detect XHR request
      $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
  
      $httpProvider.responseInterceptors.push([ '$q', '$window', '$log', '$injector', '$rootScope', '$timeout', 'I18nService', 'ApiErrorHandlerService', 'SessionService',
          function ($q, $window, $log, $injector, $rootScope, $timeout, i18n, apiErrorHandler, session) {
              return function (promise) {
                  return promise.then(
                      function httpSuccess(response) {
                          if(response &amp;&amp; response.data &amp;&amp; response.data === 'null') {
                              response.data = null;
                          }
                          return response;
                      },
                      function httpError(response) {
                          function resolveMessage(data) {
                              var msg;
                              _.each([ 'h1', 'h2', 'h3', 'h4', 'title'], function(tag) {
                                  var pattern = '&lt;' + tag + '[^&gt;]*&gt;([^&lt;]*)';
                                  var match = new RegExp(pattern).exec(data);
                                  if(match) {
                                      msg = match[1];
                                  }
                              });
                              return msg;
                          }
  
                          // Identity Provider Token timeout handling
                          var innerScope, $dialog;
                          if(response.status === 401 || response.status === 404 &amp;&amp; response.data === '') {
                              if(!handling401) {
                                  // The token refresh will cause a new 401. Because of this
                                  // we need to prevent 401 handling while a 401 handle is
                                  // in progress.
                                  handling401 = true;
  
                                  innerScope = $rootScope.$new();
                                  innerScope.error = resolveMessage(response.data);
  
                                  $dialog = $injector.get('$dialog');
                                  $dialog.dialog({
                                      // We need to keep this HTML inline because if the
                                      // client authentication token have timed out, we will
                                      // be unable to query the view using templateUrl
                                      template:   '&lt;div class="modal-header"&gt; \
                                                              &lt;h3&gt;{{ \'authentication-failed.dialog.header\' | i18n }}&lt;/h3&gt; \
                                                          &lt;/div&gt; \
                                                          &lt;div class="modal-body"&gt; \
                                                              &lt;div class="modal-body-inner"&gt; \
                                                                  &lt;div ng-show="!loaded"&gt; \
                                                                      &lt;i class="icon-spinner"&gt;&lt;/i&gt; {{ \'authentication-failed.dialog.refreshing\' | i18n }} \
                                                                  &lt;/div&gt; \
                                                                  &lt;div ng-show="iframe.status == \'fail\'" class="alert alert-error"&gt; \
                                                                      &lt;h4&gt;{{ \'authentication-failed.dialog.refresh-failed\' | i18n }}&lt;/h4&gt; \
                                                                      &lt;span ng-if="error"&gt;&lt;small&gt;{{ \'reason\' | i18n }}:&lt;br /&gt;{{ error }}&lt;/small&gt;&lt;/span&gt; \
                                                                  &lt;/div&gt; \
                                                                  &lt;div ng-show="iframe.status == \'success\'" class="alert alert-info"&gt; \
                                                                      &lt;h4&gt;{{ \'authentication-failed.dialog.refresh-successful\' | i18n }}&lt;/h4&gt; \
                                                                  &lt;/div&gt; \
                                                                  &lt;iframe av-authentication-iframe="iframe.status" style="width:500px;height:100px;display:none"&gt;&lt;/iframe&gt; \
                                                              &lt;/div&gt; \
                                                          &lt;/div&gt; \
                                                          &lt;div class="modal-footer"&gt; \
                                                              &lt;button type="button" ng-if="iframe.status == \'fail\'" onclick="location.reload(false)" class="btn"&gt;{{ \'reload\' | i18n }}&lt;/button&gt; \
                                                              &lt;button type="button" ng-if="iframe.status == \'success\'" ng-click="close()" class="btn"&gt;{{ \'close\' | i18n }}&lt;/button&gt; \
                                                          &lt;/div&gt;',
                                      controller: 'AuthenticationFailedDialogCtrl',
                                      resolve: {
                                          $scope: function() {
                                              return innerScope;
                                          }
                                      }
                                  }).open()
                                      .finally(function() {
                                          innerScope.$destroy();
                                          innerScope = null;
                                          handling401 = false;
                                      });
                              }
  
                              return $q.reject(response);
                          }
  
                          // License handling
                          if(response.status === 402) {
                              if(!$window.licenseDialogOpen) {
                                  $window.licenseDialogOpen = true;
  
                                  innerScope = $rootScope.$new();
                                  innerScope.error = response.data;
  
                                  $dialog = $injector.get('$dialog');
                                  $dialog.dialog({
                                      templateUrl: 'views/license-invalid-dialog.html',
                                      controller: 'DialogCtrl',
                                      resolve: {
                                          $scope: function() {
                                              return innerScope;
                                          }
                                      }
                                  }).open()
                                      .finally(function() {
                                          innerScope.$destroy();
                                          innerScope = null;
                                          $window.licenseDialogOpen = false;
                                      });
                              }
                              return $q.reject(response);
                          }
  
                          if (response.status === 409 &amp;&amp; apiErrorHandler.isExtensionNamespaceError(response.data)) {
                              if(apiErrorHandler.isError(response.data)) {
                                  apiErrorHandler.handle(response.data);
                              }
                              return $q.reject(response.status);
                          }
  
                          $log.error('--&gt; Intercepted failed HTTP response', response);
  
                          if(response) {
                              if(apiErrorHandler.isError(response.data)) {
                                  apiErrorHandler.handle(response.data);
                              }
                              // Ignore the error if it's an IgnoreError instance
                              else if(response.data != apiErrorHandler.IgnoreError()) {
                                  if(response.data &amp;&amp; response.data.ExceptionMessage) {
                                      $window.alert('ExceptionType: ' + response.data.ExceptionType + '\n\n' +
                                          'ExceptionMessage: ' + response.data.ExceptionMessage + '\n\n');
                                  } else {
                                      if(response.status === 0) {
                                          $log.info('Ignoring aborted XHR request.');
                                      } else {
                                          var msg = 'An unknown error with status "' + (response.status || response) + '" occurred.';
                                          if(response &amp;&amp; response.data) {
                                              if(typeof response.data == 'string') {
                                                  msg = resolveMessage(response.data);
                                              } else if(response.data.Message) {
                                                  msg = response.data.Message;
                                                  if(response.data.MessageDetail) {
                                                      msg += "\n\n" + response.data.MessageDetail;
                                                  }
                                              }
                                          }
                                          $log.error(msg);
                                          $window.alert(msg);
                                      }
                                  }
                              }
                          }
  
                          return $q.reject(response);
                      });
              };
  </code></pre>