Retry a Restangular call in an interceptor

大兔子大兔子 提交于 2019-12-03 14:31:44

Actually, the requests made with Restangular use promises that have been designed to be resolved only once. You can't resend it in case of rejection.

I don't really know if you want a re-usable errorInterceptor or not, but the way the service works don't seems to allow to re-build a Restangular object simply from the response object config.

One thing you could do is save the reference to your object and then use it in the interceptor.

You should have your reasons, but I must warn you with this because in case of failure and if it persists, you could end up with an infinite call, since the Restangular will always call the errorInterceptor.

I've mocked up a little Plunkr for the demonstration, just open your network tab and click the button, you should see all the request goes trough there.

Using async.retry may fit to your problem.

Let's assume your asynchronous function is :

function apiCall(callback) {
  Restangular.all('items').getList()
    .then(function (res) {
      // all is OK
      callback(null, res);
    })
    .catch(function (err) {
      if (err.status === 403) {
        // this one will bring async to retry apiMethod
        return callback(err);
      }
      // others errors codes will not call apiMethod again
      callback(null);
    });
}

Example using retry :

// async will try it 3 times
async.retry(3, apiCall, function(err, result) {
  // do something with the result
});

async.doUntil could interest you too.

It is too late for an answer, however I found a solution for this, So I am going to mention it here as possible solution.

There is a hint in the following link which says you need to use restangularizeElement.

https://github.com/mgonto/restangular/issues/1022

I am fixing restangular official example as follow:

Restangular.setErrorInterceptor(function(response, deferred, responseHandler) {
  if(response.status === 403) {
    refreshAccesstoken().then(function() {
      // instead of using $http, I will use restangularizeElement as follow
      // I still have no idea how is the best method to repeat the request after restangularized it. I am repeating it with a simple get
      var request = Restangular.restangularizeElement('', response.data, url);
      request.get().then(function() {});
    });
    return false; // error handled
  }

  return true; // error not handled
});

In this way, if repeated request cause any error, it will be caught in restangular error interceptor again.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!