Skip to content

Commit

Permalink
fix: Wrap localStorage calls in try catch
Browse files Browse the repository at this point in the history
  • Loading branch information
jknoxville committed Mar 23, 2021
1 parent 7834948 commit 92bb99c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,18 @@ const useAnnouncementBar = (): useAnnouncementBarReturns => {

const [isClosed, setClosed] = useState(true);

let localStorageAvailable = true;
try {
localStorage.getItem;
} catch (err) {
console.error(err);
localStorageAvailable = false;
}

const handleClose = useCallback(() => {
if (!localStorageAvailable) {
return;
}
localStorage.setItem(STORAGE_DISMISS_KEY, 'true');
setClosed(true);
}, []);
Expand All @@ -26,6 +37,9 @@ const useAnnouncementBar = (): useAnnouncementBarReturns => {
if (!announcementBar) {
return;
}
if (!localStorageAvailable) {
return;
}
const {id} = announcementBar;

let viewedId = localStorage.getItem(STORAGE_ID_KEY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ const DocsPreferredVersionStorage = {
if (persistence === 'none') {
// noop
} else {
window.localStorage.setItem(storageKey(pluginId), versionName);
try {
window.localStorage.setItem(storageKey(pluginId), versionName);
} catch (err) {
console.error(err);
}
}
},

Expand All @@ -29,15 +33,24 @@ const DocsPreferredVersionStorage = {
if (persistence === 'none') {
return null;
} else {
return window.localStorage.getItem(storageKey(pluginId));
try {
return window.localStorage.getItem(storageKey(pluginId));
} catch (err) {
console.error(err);
return null;
}
}
},

clear: (pluginId: string, persistence: DocsVersionPersistence): void => {
if (persistence === 'none') {
// noop
} else {
window.localStorage.removeItem(storageKey(pluginId));
try {
window.localStorage.removeItem(storageKey(pluginId));
} catch (err) {
console.error(err);
}
}
},
};
Expand Down

0 comments on commit 92bb99c

Please sign in to comment.