From dbfd45794a76d05a4a338fda5eab14d88c2dde0d Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Thu, 7 Sep 2023 21:03:15 -0700 Subject: [PATCH] More safely parse JSON from localStorage --- src/lib/LocalStorage.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/lib/LocalStorage.js b/src/lib/LocalStorage.js index 3dc3458..24b8711 100644 --- a/src/lib/LocalStorage.js +++ b/src/lib/LocalStorage.js @@ -40,7 +40,12 @@ class UserSettings { case Boolean: this[key] = localStorage[key] === "true"; case Object: - this[key] = JSON.parse(localStorage[key]); + try { + this[key] = JSON.parse(localStorage[key]); + } catch(e) { + console.error(`LocalStorage: parsing key ${key}: ${e}`); + delete(this[key]); + } } } }