I want to retry a call after some condition with Restangular 1.4.0. So I set up a setErrorInterceptor
as in the documentation. It works but the then
clause of the first call is not executed. The responseHandler
returns undefined
in the error interceptor.
My client code:
Restangular.setErrorInterceptor(function (response, deferred, responseHandler) {
console.log('error interceptor');
console.log(response.status);
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
} else if (response.status === 401) {
return $http.get('http://192.168.0.27:3000/auth/guest').then(function (result) {
var token = result.data.token;
Auth.setToken(token);
response.config.headers.Authorization = 'Bearer ' + Auth.getToken();
console.log(responseHandler);
//response.config.headers["Authorization"] = Auth.getToken();
$http(response.config).then(responseHandler, deferred.reject);
//return token;
return false;
});
//$http(response.config).then(responseHandler, deferred.reject);
}
console.log('error not handled');
return true; // error not handled
});
Dashboards.post()
.then(function (result) {
console.log(result);
return Restangular.oneUrl('newDash', result.data).get();
}, function (response) {
console.log("Error with status code", response.status);
})
.then(function (result) {
console.log(result);
});
My server log:
POST /api/v1/dashboards HTTP/1.1" 401
GET /auth/guest HTTP/1.1" 200
POST /api/v1/dashboards HTTP/1.1" 201