From 5af63ce03a85985981166527b9604b8a0226fed8 Mon Sep 17 00:00:00 2001 From: Ray Schamp Date: Fri, 2 Oct 2015 15:27:57 -0400 Subject: [PATCH] Handle errors by retrying with exponential backoff --- src/mixins/api.jsx | 8 +++++++- src/session.js | 13 ++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/mixins/api.jsx b/src/mixins/api.jsx index cdb54b360..d542f5e05 100644 --- a/src/mixins/api.jsx +++ b/src/mixins/api.jsx @@ -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)); } }; diff --git a/src/session.js b/src/session.js index bff716391..ebcfd9066 100644 --- a/src/session.js +++ b/src/session.js @@ -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); + } }); };