From 24ce3606a36af6eec709fa45af33cf6e53417c6d Mon Sep 17 00:00:00 2001 From: Ariel Salem Date: Fri, 22 May 2020 10:13:17 -0700 Subject: [PATCH] chore(hb_parse_error): created a JSON validation function to prevent HB error and correctly log errors (#18199) --- ui/src/localStorage.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/ui/src/localStorage.ts b/ui/src/localStorage.ts index f7195c4da6..667bb3260d 100644 --- a/ui/src/localStorage.ts +++ b/ui/src/localStorage.ts @@ -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) } }