mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-20 04:16:00 +00:00
fixed crash when there is invalid JSON in local storage
This commit is contained in:
@@ -18,9 +18,18 @@ export interface TabDefinition {
|
||||
tabOrder?: number;
|
||||
}
|
||||
|
||||
function safeJsonParse(json, defaultValue) {
|
||||
try {
|
||||
return JSON.parse(json);
|
||||
} catch (err) {
|
||||
console.error(`Error parsing JSON value "${json}"`, err);
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
export function writableWithStorage<T>(defaultValue: T, storageName) {
|
||||
const init = localStorage.getItem(storageName);
|
||||
const res = writable<T>(init ? JSON.parse(init) : defaultValue);
|
||||
const res = writable<T>(init ? safeJsonParse(init, defaultValue) : defaultValue);
|
||||
res.subscribe(value => {
|
||||
localStorage.setItem(storageName, JSON.stringify(value));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user