Skip to content

Commit

Permalink
chore(hb_parse_error): created a JSON validation function to prevent …
Browse files Browse the repository at this point in the history
…HB error and correctly log errors (#18199)
  • Loading branch information
asalem1 authored May 22, 2020
1 parent 083f72a commit 24ce360
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions ui/src/localStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 24ce360

Please sign in to comment.