Handle errors by retrying with exponential backoff

This commit is contained in:
Ray Schamp 2015-10-02 15:27:57 -04:00
parent 66f654536b
commit 5af63ce03a
2 changed files with 17 additions and 4 deletions

View file

@ -1,14 +1,20 @@
var xhr = require('xhr');
module.exports = {
ERR_500: 500,
api: function (opts, callback) {
xhr(opts, function (err, res, body) {
if (err) {
// emit global "error" event
return callback(err);
}
if (res.statusCode == 500) {
return callback(this.ERR_500);
}
// @todo Global error handler
callback(err, body);
});
}.bind(this));
}
};

View file

@ -9,12 +9,19 @@ window.updateSession = function (session) {
window.dispatchEvent(sessionEvent);
};
window.refreshSession = function () {
window.refreshSession = function (iteration) {
if (!iteration) iteration = 1;
api({
uri: '/session/',
responseType: 'json'
}, function (err, res) {
if (!err) window.updateSession(res);
}, function (err, body) {
if (err) {
var timeout = Math.floor(Math.pow(Math.E, iteration));
if (!isFinite(timeout)) return;
window.setTimeout(window.refreshSession.bind(window, iteration+1), timeout);
} else {
window.updateSession(body);
}
});
};