From ce286cc092829e7fddd45c77a6a2febfa2a97fd1 Mon Sep 17 00:00:00 2001 From: Tilman Vatteroth Date: Mon, 9 Oct 2023 07:09:13 +0200 Subject: [PATCH] fix: catch access forbidden errors for local storage access Signed-off-by: Tilman Vatteroth --- .../application-loader/initializers/index.ts | 19 +++++++++++++------ frontend/src/redux/history/methods.ts | 16 ++++++++++++++-- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/frontend/src/components/application-loader/initializers/index.ts b/frontend/src/components/application-loader/initializers/index.ts index eeaeb6c22..6e4fe946a 100644 --- a/frontend/src/components/application-loader/initializers/index.ts +++ b/frontend/src/components/application-loader/initializers/index.ts @@ -17,18 +17,25 @@ const logger = new Logger('Application Loader') * Create a custom delay in the loading of the application. */ const customDelay: () => Promise = async () => { - if ( - (isDevMode || isTestMode) && - typeof window !== 'undefined' && - typeof window.localStorage !== 'undefined' && - (window.location.search.startsWith('?customDelay=') || window.localStorage.getItem('customDelay')) - ) { + if ((isDevMode || isTestMode) && (window.location.search.startsWith('?customDelay=') || isCustomDelayActive())) { return new Promise((resolve) => setTimeout(resolve, 500000000)) } else { return Promise.resolve() } } +const isCustomDelayActive = (): boolean => { + try { + return ( + typeof window !== 'undefined' && + typeof window.localStorage !== 'undefined' && + window.localStorage.getItem('customDelay') !== null + ) + } catch { + return false + } +} + export interface InitTask { name: string task: () => Promise diff --git a/frontend/src/redux/history/methods.ts b/frontend/src/redux/history/methods.ts index 5052458eb..d1bf5282d 100644 --- a/frontend/src/redux/history/methods.ts +++ b/frontend/src/redux/history/methods.ts @@ -185,7 +185,11 @@ export const storeLocalHistory = (): void => { ...entry, origin: undefined })) - window.localStorage.setItem('history', JSON.stringify(entriesWithoutOrigin)) + try { + window.localStorage.setItem('history', JSON.stringify(entriesWithoutOrigin)) + } catch (error) { + log.error("Can't save history", error) + } } /** @@ -206,7 +210,7 @@ export const storeRemoteHistory = (): Promise => { * @return The local history entries with the origin set to local. */ const loadLocalHistory = (): HistoryEntryWithOrigin[] => { - const localV1Json = window.localStorage.getItem('notehistory') + const localV1Json = readV1HistoryEntriesFromLocalStorage() if (localV1Json) { try { const localV1History = JSON.parse(JSON.parse(localV1Json) as string) as V1HistoryEntry[] @@ -235,6 +239,14 @@ const loadLocalHistory = (): HistoryEntryWithOrigin[] => { } } +const readV1HistoryEntriesFromLocalStorage = () => { + try { + return window.localStorage.getItem('notehistory') + } catch { + return null + } +} + /** * Loads the remote history and maps each entry with a remote origin label. * @return The remote history entries with the origin set to remote.