title: rxJs & angular 4 & restangular: stack errorInterceptors
body: <p>In my angular 4 app I'm using <code>ngx-restangular</code> to handle all server calls. It returns observable as result, and this module has hooks to handle errors (like 401 etc).</p>
  
  <p>But from documentation, i can handle 403 (401) so:</p>
  
  <pre><code>  RestangularProvider.addErrorInterceptor((response, subject, responseHandler) =&gt; {
      if (response.status === 403) {
  
        refreshAccesstoken()
        .switchMap(refreshAccesstokenResponse =&gt; {
          //If you want to change request or make with it some actions and give the request to the repeatRequest func.
          //Or you can live it empty and request will be the same.
  
          // update Authorization header
          response.request.headers.set('Authorization', 'Bearer ' + refreshAccesstokenResponse)
  
          return response.repeatRequest(response.request);
        })
        .subscribe(
          res =&gt; responseHandler(res),
          err =&gt; subject.error(err)
        );
  
        return false; // error handled
      }
      return true; // error not handled
    });
  </code></pre>
  
  <p>and this is good for one request, which has broken with 403 error.
  how can i stack this calls using rxJs? Becouse now, for example, i have 3 requests, which have 403 and for each this broken request I'm refreshing token - this is not so good, i have to update my token and then repeat all my broken requests. How can I achive this using Observables?</p>
  
  <p>In angular 1 it was pretty easy:</p>
  
  <pre><code>Restangular.setErrorInterceptor(function (response, deferred, responseHandler) {
    if (response.status == 403) {
      // send only one request if multiple errors exist
      if (!refreshingIsInProgress) {
        refreshingIsInProgress = AppApi.refreshAccessToken(); // Returns promise
      }
  
  
      $q.when(refreshingIsInProgress, function () {
        refreshingIsInProgress = null;
  
        setHeaders(response.config.headers);
  
        // repeat request with error
        $http(response.config).then(responseHandler, deferred);
      }, function () {
        refreshingIsInProgress = null;
  
        $state.go('auth');
      });
  
      return false; // stop the promise chain
    }
  
    return true;
  });
  </code></pre>
  
  <p>And all was working like a charm. But I'm new to rxJs &amp; angular 4 and I don't have any idea how to achive this with observables and angular 4. Maybe somebody have an idea?</p>
  
  <p>upd!
  here is my refreshAccesstoken method</p>
  
  <pre><code>const refreshAccesstoken = function () {
    const refreshToken = http.post(environment.apiURL + `/token/refresh`,
      {refreshToken: 'someToken'});
    return refreshToken;
  };
  </code></pre>