chore(hb_parse_error): created a JSON validation function to prevent HB error and correctly log errors (#18199)

pull/18204/head
Ariel Salem 2020-05-22 10:13:17 -07:00 committed by GitHub
parent 083f72a514
commit 24ce3606a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 2 deletions

View File

@ -29,13 +29,23 @@ export const loadLocalStorage = (): LocalStorage => {
}
}
const isValidJSONString = errorString => {
try {
JSON.parse(errorString)
} catch (e) {
return false
}
return true
}
export const saveToLocalStorage = (state: LocalStorage): void => {
try {
window.localStorage.setItem(
'state',
JSON.stringify(normalizeSetLocalStorage(state))
)
} catch (err) {
console.error('Unable to save state to local storage: ', JSON.parse(err))
} catch (error) {
const errorMessage = isValidJSONString(error) ? JSON.parse(error) : error
console.error('Unable to save state to local storage: ', errorMessage)
}
}