javascript - Angularjs $http interceptors - Cannot generate call to requestError -


i'm using $http interceptors capture events following ajax submission. reason, not able throw requesterror. i've set test app try , call requesterror, far can multiple responseerrors.

from angularjs docs:

requesterror: interceptor gets called when previous interceptor threw error or resolved rejection.

this test code.

        .factory('httpinterceptor',['$q',function(q){              var interceptor = {};              var uniqueid = function uniqueid() {                 return new date().gettime().tostring(16) + '.' + (math.round(math.random() * 100000)).tostring(16);             };              interceptor.request = function(config){                 config.id = uniqueid();                 console.log('request ',config.id,config);                 return config;             };              interceptor.response = function(response){                 console.log('response',response);                 return response;                                 };              interceptor.requesterror = function(config){                 console.log('requesterror ',config.id,config);                 return q.reject(config);             };              interceptor.responseerror = function(response){                 console.log('responseerror ',response.config.id,response);                 return q.reject(response);                                 };              return interceptor;          }])          .config(['$httpprovider',function($httpprovider) {             $httpprovider.interceptors.push('httpinterceptor');         }])          .controller('mainctrl',['$http',function($http){              var mainctrl = this;              mainctrl.method = null;             mainctrl.url = null;              var testhttp = function testhttp() {                  $http({method:mainctrl.method,url:mainctrl.url}).then(                         (response)=>{console.log('ok',response);},                         (response)=>{console.log('reject',response);}                 );             };              //api             mainctrl.testhttp = testhttp;          }]) 

i've tried multiple ways of creating http errors, , every time responseerror gets called. things i've tried:

  • get server return different types of error every request, e.g. 400 , 500.
  • get server sleep random times, later requests respond error before earlier requests. same resource, same server response.
  • generate 404 errors requesting resources don't exist.
  • disconnecting internet (responseerror -1).

similar questions

1) question seems have answer: when functions request, requesterror, response, responseerror invoked when intercepting http request , response?

the key paragrapgh being:

a key point of above methods can return either "normal" object/primitive or promise resolve appropriate value. in latter case, next interceptor in queue wait until returned promise resolved or rejected.

but think i'm doing stipulates, viz random sleep server no luck. getting reponseerrors out of order request ie server responds.

2) similar question asked 1 year ago: angular , jasmine: how test requesterror / rejection in http interceptor?

unfortunately, provides explanation interceptors. not answer question.

i have tested in chrome , firefox. hope understand, i've done best find solution this, haven't come across solution yet.

this happens because request isn't rejected @ point. supposed used like that:

app.factory('interceptor1', ['$q', function ($q) {   return {     request: function (config) {       console.log('request', config);       if (config.url === 'restricted')         return $q.reject({ error: 'restricted', config: config });     }   }; }]);  app.factory('interceptor2', ['$q', function ($q) {   return {     requesterror: function (rejection) {       console.log('requesterror', rejection);             if (rejection.error === 'restricted')         return angular.extend(rejection.config, { url: 'allowed' });        return $q.reject(rejection);     }   }; }]);  app.config(['$httpprovider',function($httpprovider) {   $httpprovider.interceptors.push('interceptor1');   $httpprovider.interceptors.push('interceptor2'); }]); 

notice interceptors supposed work in stack (starting transform* hooks in $http request), request can't rejected , recovered within single interceptor.


Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -