From e0ffb6e9f3ba8f1ebac21696a36d559f2b0dd9d8 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Tue, 6 Sep 2022 10:43:41 +0530 Subject: [PATCH] add error handling for on mount useEffect of AuthenticateUserModal --- src/components/AuthenticateUserModal.tsx | 33 +++++++++++++++--------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/components/AuthenticateUserModal.tsx b/src/components/AuthenticateUserModal.tsx index d9399cfd6..fd8a2ee8b 100644 --- a/src/components/AuthenticateUserModal.tsx +++ b/src/components/AuthenticateUserModal.tsx @@ -8,6 +8,7 @@ import VerifyMasterPasswordForm, { VerifyMasterPasswordFormProps, } from 'components/VerifyMasterPasswordForm'; import { Dialog, Stack, Typography } from '@mui/material'; +import { logError } from 'utils/sentry'; interface Iprops { open: boolean; @@ -33,19 +34,27 @@ export default function AuthenticateUserModal({ useEffect(() => { const main = async () => { - const user = getData(LS_KEYS.USER); - setUser(user); - const keyAttributes = getData(LS_KEYS.KEY_ATTRIBUTES); - - if ( - (!user?.token && !user?.encryptedToken) || - (keyAttributes && !keyAttributes.memLimit) - ) { + try { + const user = getData(LS_KEYS.USER); + if (!user) { + throw Error('User not found'); + } + setUser(user); + const keyAttributes = getData(LS_KEYS.KEY_ATTRIBUTES); + if ( + (!user?.token && !user?.encryptedToken) || + (keyAttributes && !keyAttributes.memLimit) + ) { + throw Error('User not logged in'); + } else if (!keyAttributes) { + throw Error('Key attributes not found'); + } else { + setKeyAttributes(keyAttributes); + } + } catch (e) { + logError(e, 'AuthenticateUserModal initialization failed'); + onClose(); somethingWentWrong(); - } else if (!keyAttributes) { - somethingWentWrong(); - } else { - setKeyAttributes(keyAttributes); } }; main();