mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-22 22:46:01 +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;
|
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) {
|
export function writableWithStorage<T>(defaultValue: T, storageName) {
|
||||||
const init = localStorage.getItem(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 => {
|
res.subscribe(value => {
|
||||||
localStorage.setItem(storageName, JSON.stringify(value));
|
localStorage.setItem(storageName, JSON.stringify(value));
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user