mirror of
https://github.com/scratchfoundation/scratch-www.git
synced 2024-11-26 17:16:11 -05:00
Handle errors by retrying with exponential backoff
This commit is contained in:
parent
66f654536b
commit
5af63ce03a
2 changed files with 17 additions and 4 deletions
|
@ -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));
|
||||
}
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue