More safely parse JSON from localStorage

vue-cli
Noah 2023-09-07 21:03:15 -07:00
parent a2cb32cce2
commit dbfd45794a
1 changed files with 6 additions and 1 deletions

View File

@ -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]);
}
}
}
}