Skip to content

Commit 8f36646

Browse files
committed
json parce decorated with try catch
1 parent 5747e14 commit 8f36646

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/infrastructure/storage/abstract/persistant.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,16 @@ export class PersistantStore<StoreData extends Record<string, unknown>> extends
3434
get: (target, prop) => {
3535
const item = this.storage.getItem(prop as string);
3636

37-
return item !== null ? JSON.parse(item) : undefined;
37+
try {
38+
return item !== null ? JSON.parse(item) : undefined;
39+
} catch {
40+
console.warn(`Persistant store: Cannot parse ${item}`);
41+
42+
/**
43+
* Delete item that cannot be parsed
44+
*/
45+
this.storage.removeItem(prop as string);
46+
}
3847
},
3948

4049
set: (target, prop, value, receiver) => {
@@ -74,7 +83,16 @@ export class PersistantStore<StoreData extends Record<string, unknown>> extends
7483
const storedValue = this.storage.getItem(key);
7584

7685
if (storedValue !== null) {
77-
this.data[key as keyof StoreData] = JSON.parse(storedValue);
86+
try {
87+
this.data[key as keyof StoreData] = JSON.parse(storedValue);
88+
} catch {
89+
console.warn(`Persistant store: Cannot parse ${storedValue}`);
90+
91+
/**
92+
* Delete item that cannot be parsed
93+
*/
94+
this.storage.removeItem(key);
95+
}
7896
}
7997
}
8098
}

0 commit comments

Comments
 (0)