diff --git a/r2/r2/public/static/js/base.js b/r2/r2/public/static/js/base.js index b839c0c76..0e7688898 100644 --- a/r2/r2/public/static/js/base.js +++ b/r2/r2/public/static/js/base.js @@ -36,6 +36,35 @@ r.ajax = function(request) { return $.ajax(request) } +store.safeGet = function(key, errorValue) { + // errorValue defaults to undefined, equivalent to the key being unset. + try { + return store.get(key) + } catch (err) { + r.sendError('Unable to read storage key "%(key)s" (%(err)s)'.format({ + key: key, + err: err + })) + // TODO: reset value to errorValue? + return errorValue + } +} + +store.safeSet = function(key, val) { + // swallow exceptions upon storage set for non-trivial operations. returns + // a boolean value indicating success. + try { + store.set(key, val) + return true + } catch (err) { + r.warn('Unable to set storage key "%(key)s" (%(err)s)'.format({ + key: key, + err: err + })) + return false + } +} + r.setupBackbone = function() { Backbone.emulateJSON = true Backbone.ajax = r.ajax diff --git a/r2/r2/public/static/js/multi.js b/r2/r2/public/static/js/multi.js index c9479ce16..56d898d09 100644 --- a/r2/r2/public/static/js/multi.js +++ b/r2/r2/public/static/js/multi.js @@ -610,9 +610,9 @@ r.multi.ListingChooser = Backbone.View.extend({ this.$el.addClass('initialized') // transition collapsed state to server pref - if (store.get('ui.collapse.listingchooser') == true) { + if (store.safeGet('ui.collapse.listingchooser') == true) { this.toggleCollapsed(true) - store.remove('ui.collapse.listingchooser') + store.safeSet('ui.collapse.listingchooser') } // HACK: fudge page heights for long lists of multis / short pages diff --git a/r2/r2/public/static/js/ui.js b/r2/r2/public/static/js/ui.js index 12c3a1197..8ce703cd6 100644 --- a/r2/r2/public/static/js/ui.js +++ b/r2/r2/public/static/js/ui.js @@ -3,10 +3,10 @@ r.ui.init = function() { if ($.cookie('reddit_first')) { // save welcome seen state and delete obsolete cookie $.cookie('reddit_first', null, {domain: r.config.cur_domain}) - store.set('ui.shown.welcome', true) - } else if (store.get('ui.shown.welcome') != true) { + store.safeSet('ui.shown.welcome', true) + } else if (store.safeGet('ui.shown.welcome') != true) { $('.infobar.welcome').show() - store.set('ui.shown.welcome', true) + store.safeSet('ui.shown.welcome', true) } // mobile suggest infobar diff --git a/r2/r2/public/static/js/uibase.js b/r2/r2/public/static/js/uibase.js index ee21f5a7a..4733cab3d 100644 --- a/r2/r2/public/static/js/uibase.js +++ b/r2/r2/public/static/js/uibase.js @@ -13,7 +13,7 @@ r.ui.Collapse = function(el, target, key) { r.ui.Base.call(this, el) this.target = target this.key = 'ui.collapse.' + key - this.isCollapsed = store.get(this.key) == true + this.isCollapsed = store.safeGet(this.key) == true this.$el.click($.proxy(this, 'toggle', null, false)) this.toggle(this.isCollapsed, true) } @@ -33,7 +33,7 @@ r.ui.Collapse.prototype = { } this.isCollapsed = collapsed - store.set(this.key, collapsed) + store.safeSet(this.key, collapsed) this.update() },