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

@ -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<unknown> => {
* @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.