Catch and log errors setting local storage values.

This commit is contained in:
Max Goodman
2013-09-06 17:22:29 -07:00
parent 22a5328422
commit b89faa152b
4 changed files with 36 additions and 7 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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()
},