Skip to content

Commit 655852c

Browse files
author
Marc-André Arseneault
committed
Prevent JSON parse error on _get
In order to prevent JSON parse error on `_get`, I suggest to put the item parsing `JSON.parse(storage.getItem(key));` on a try-catch and set the item value to `null` if the parsing fail. This error occur when the localstorage is used by the cross-storage module (which set the localstorage value into an object like `{"value":null}`) an a native usage of the localstorage (which not necessary set the localstorage value into an object and will break the JSON parsing).
1 parent 759672d commit 655852c

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

lib/hub.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,12 @@
188188

189189
for (i = 0; i < params.keys.length; i++) {
190190
key = params.keys[i];
191-
item = JSON.parse(storage.getItem(key));
191+
192+
try {
193+
item = JSON.parse(storage.getItem(key));
194+
} catch (e) {
195+
item = null;
196+
}
192197

193198
if (item === null) {
194199
result.push(null);

0 commit comments

Comments
 (0)