Retry intercept callback

The official relational documentation provides this sample code to repeat the request in ErrorInterceptor:

var refreshAccesstoken = function() { var deferred = $q.defer(); // Refresh access-token logic return deferred.promise; }; Restangular.setErrorInterceptor(function(response, deferred, responseHandler) { if(response.status === 403) { refreshAccesstoken().then(function() { // Repeat the request and then call the handlers the usual way. $http(response.config).then(responseHandler, deferred.reject); // Be aware that no request interceptors are called this way. }); return false; // error handled } return true; // error not handled }); 

However, as stated in the comments, the second attempt will not fire any sniffers, as it uses $ http directly.

Is there a way to make the second request also go through the relay pipeline and execute the ErrorInterceptor?

+6
source share
3 answers

In fact, requests made using relay using promises were designed to be resolved only once. You cannot resend it in case of failure.

I really don't know if you want to reuse errorInterceptor or not, but the way the service works does not seem to allow rebuilding the Restangular object simply from the response object config .

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

You must have your own reasons, but I must warn you about it, because in the event of a failure, and if it persists, you can receive an endless call, since Restangular will always call errorInterceptor .

I mocked a little Plunkr for a demonstration, just open your network tab and click the button, you should see that all requests go there.

+4
source

Using async.retry may be appropriate for your problem.

Assume your asynchronous function:

 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); }); } 

retry :

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

async.doUntil may interest you.

+1
source

Too late for an answer, however I found a solution for this, so I am going to mention it here as a possible solution.

The following link has a hint that says you need to use restangularizeElement .

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

I fix the restatular official example as follows:

 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 }); 

Thus, if a repeated request causes an error, it again falls into the interceptor of tunable errors.

+1
source

Source: https://habr.com/ru/post/974978/


All Articles