Skip to content

Commit

Permalink
Prevent JSON parse error on _get
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
Marc-André Arseneault committed Mar 22, 2016
1 parent 759672d commit 655852c
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/hub.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,12 @@

for (i = 0; i < params.keys.length; i++) {
key = params.keys[i];
item = JSON.parse(storage.getItem(key));

try {
item = JSON.parse(storage.getItem(key));
} catch (e) {
item = null;
}

if (item === null) {
result.push(null);
Expand Down

0 comments on commit 655852c

Please sign in to comment.