mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-30 06:25:28 -05:00
1213709578
[web] migrate frontend logging to debugConsole GitOrigin-RevId: b03ff9030b03fff059738d2270bc4b4912e79f16
47 lines
1.2 KiB
JavaScript
47 lines
1.2 KiB
JavaScript
/**
|
|
* sessionStorage can throw browser exceptions, for example if it is full.
|
|
* We don't use sessionStorage for anything critical, so in that case just fail gracefully.
|
|
*/
|
|
|
|
import { debugConsole } from '@/utils/debugging'
|
|
|
|
/**
|
|
* Catch, log and otherwise ignore errors.
|
|
*
|
|
* @param {function} fn sessionStorage function to call
|
|
* @param {string?} key Key passed to the sessionStorage function (if any)
|
|
* @param {any?} value Value passed to the sessionStorage function (if any)
|
|
*/
|
|
const callSafe = function (fn, key, value) {
|
|
try {
|
|
return fn(key, value)
|
|
} catch (e) {
|
|
debugConsole.error('sessionStorage exception', e)
|
|
return null
|
|
}
|
|
}
|
|
|
|
const getItem = function (key) {
|
|
return JSON.parse(sessionStorage.getItem(key))
|
|
}
|
|
|
|
const setItem = function (key, value) {
|
|
sessionStorage.setItem(key, JSON.stringify(value))
|
|
}
|
|
|
|
const clear = function () {
|
|
sessionStorage.clear()
|
|
}
|
|
|
|
const removeItem = function (key) {
|
|
return sessionStorage.removeItem(key)
|
|
}
|
|
|
|
const customSessionStorage = {
|
|
getItem: key => callSafe(getItem, key),
|
|
setItem: (key, value) => callSafe(setItem, key, value),
|
|
clear: () => callSafe(clear),
|
|
removeItem: key => callSafe(removeItem, key),
|
|
}
|
|
|
|
export default customSessionStorage
|