fix: catch access forbidden errors for local storage access

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Tilman Vatteroth 2023-10-09 07:09:13 +02:00
parent be3ca5cdeb
commit ce286cc092
2 changed files with 27 additions and 8 deletions

View file

@ -17,18 +17,25 @@ const logger = new Logger('Application Loader')
* Create a custom delay in the loading of the application. * Create a custom delay in the loading of the application.
*/ */
const customDelay: () => Promise<void> = async () => { const customDelay: () => Promise<void> = async () => {
if ( if ((isDevMode || isTestMode) && (window.location.search.startsWith('?customDelay=') || isCustomDelayActive())) {
(isDevMode || isTestMode) &&
typeof window !== 'undefined' &&
typeof window.localStorage !== 'undefined' &&
(window.location.search.startsWith('?customDelay=') || window.localStorage.getItem('customDelay'))
) {
return new Promise((resolve) => setTimeout(resolve, 500000000)) return new Promise((resolve) => setTimeout(resolve, 500000000))
} else { } else {
return Promise.resolve() 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 { export interface InitTask {
name: string name: string
task: () => Promise<void> task: () => Promise<void>

View file

@ -185,7 +185,11 @@ export const storeLocalHistory = (): void => {
...entry, ...entry,
origin: undefined origin: undefined
})) }))
try {
window.localStorage.setItem('history', JSON.stringify(entriesWithoutOrigin)) window.localStorage.setItem('history', JSON.stringify(entriesWithoutOrigin))
} catch (error) {
log.error("Can't save history", error)
}
} }
/** /**
@ -206,7 +210,7 @@ export const storeRemoteHistory = (): Promise<unknown> => {
* @return The local history entries with the origin set to local. * @return The local history entries with the origin set to local.
*/ */
const loadLocalHistory = (): HistoryEntryWithOrigin[] => { const loadLocalHistory = (): HistoryEntryWithOrigin[] => {
const localV1Json = window.localStorage.getItem('notehistory') const localV1Json = readV1HistoryEntriesFromLocalStorage()
if (localV1Json) { if (localV1Json) {
try { try {
const localV1History = JSON.parse(JSON.parse(localV1Json) as string) as V1HistoryEntry[] 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. * Loads the remote history and maps each entry with a remote origin label.
* @return The remote history entries with the origin set to remote. * @return The remote history entries with the origin set to remote.