From 7082d761dc07940259b954acf306679507f86b12 Mon Sep 17 00:00:00 2001 From: Alka Date: Thu, 16 Nov 2023 11:04:33 +0530 Subject: [PATCH] feat(INJI-560): add error events at multiple places to catch the error scenarios Signed-off-by: Alka --- components/PasscodeVerify.tsx | 12 ++++++ machines/store.ts | 52 +++++++++++++++++++++++++ screens/Home/MyVcs/AddVcModalMachine.ts | 12 +++++- shared/storage.ts | 23 +++++++++++ shared/telemetry/TelemetryConstants.js | 3 ++ 5 files changed, 101 insertions(+), 1 deletion(-) diff --git a/components/PasscodeVerify.tsx b/components/PasscodeVerify.tsx index 0f58edc8c5..3bdac095b3 100644 --- a/components/PasscodeVerify.tsx +++ b/components/PasscodeVerify.tsx @@ -3,6 +3,11 @@ import {useTranslation} from 'react-i18next'; import {PinInput} from './PinInput'; import {hashData} from '../shared/commonUtil'; import {argon2iConfig} from '../shared/constants'; +import { + getErrorEventData, + sendErrorEvent, +} from '../shared/telemetry/TelemetryUtils'; +import {TelemetryConstants} from '../shared/telemetry/TelemetryConstants'; export const MAX_PIN = 6; @@ -32,6 +37,13 @@ export const PasscodeVerify: React.FC = props => { } } } catch (error) { + sendErrorEvent( + getErrorEventData( + TelemetryConstants.FlowType.appLogin, + TelemetryConstants.ErrorId.mismatch, + error, + ), + ); console.log('error:', error); } } diff --git a/machines/store.ts b/machines/store.ts index 1516bd1d0f..9e35979709 100644 --- a/machines/store.ts +++ b/machines/store.ts @@ -284,6 +284,8 @@ export const storeMachine = 'Dummy', ); } catch (e) { + sendErrorEvent(getErrorEventData('ENCRYPTION', '', e)); + if (e.message.includes(keyinvalidatedString)) { await clear(); callback(model.events.KEY_INVALIDATE_ERROR()); @@ -294,6 +296,13 @@ export const storeMachine = } callback(model.events.READY()); } else { + sendErrorEvent( + getErrorEventData( + 'ENCRYPTION', + '', + 'Could not get the android Key alias', + ), + ); callback( model.events.ERROR( new Error('Could not get the android Key alias'), @@ -400,6 +409,14 @@ export const storeMachine = } callback(model.events.STORE_RESPONSE(response, event.requester)); } catch (e) { + sendErrorEvent( + getErrorEventData( + TelemetryConstants.FlowType.fetchData, + '', + e.message, + {e}, + ), + ); if (e.message.includes(keyinvalidatedString)) { await clear(); callback(model.events.KEY_INVALIDATE_ERROR()); @@ -432,6 +449,13 @@ export const storeMachine = console.log('Credentials successfully loaded for user'); callback(model.events.KEY_RECEIVED(existingCredentials.password)); } else { + sendErrorEvent( + getErrorEventData( + TelemetryConstants.FlowType.fetchData, + '', + 'Could not get keychain credentials', + ), + ); console.log('Credentials failed to load for user'); callback( model.events.ERROR( @@ -452,6 +476,13 @@ export const storeMachine = if (hasSetCredentials) { callback(model.events.KEY_RECEIVED(randomBytesString)); } else { + sendErrorEvent( + getErrorEventData( + TelemetryConstants.FlowType.fetchData, + '', + 'Could not generate keychain credentials', + ), + ); callback( model.events.ERROR( new Error('Could not generate keychain credentials.'), @@ -532,6 +563,13 @@ export async function getItem( } if (data === null && VCMetadata.isVCKey(key)) { await removeItem(key, data, encryptionKey); + sendErrorEvent( + getErrorEventData( + TelemetryConstants.FlowType.fetchData, + TelemetryConstants.ErrorId.tampered, + tamperedErrorMessageString, + ), + ); throw new Error(tamperedErrorMessageString); } else { return defaultValue; @@ -544,8 +582,22 @@ export async function getItem( e instanceof BiometricCancellationError || e.message.includes('Key not found') // this error happens when previous get Item calls failed due to key invalidation and data and keys are deleted ) { + sendErrorEvent( + getErrorEventData( + TelemetryConstants.FlowType.fetchData, + TelemetryConstants.ErrorId.tampered, + e.message, + ), + ); throw e; } + sendErrorEvent( + getErrorEventData( + TelemetryConstants.FlowType.fetchData, + TelemetryConstants.ErrorId.tampered, + `Exception in getting item for ${key}: ${e}`, + ), + ); console.error(`Exception in getting item for ${key}: ${e}`); return defaultValue; } diff --git a/screens/Home/MyVcs/AddVcModalMachine.ts b/screens/Home/MyVcs/AddVcModalMachine.ts index 3c9a2df483..6042d238b1 100644 --- a/screens/Home/MyVcs/AddVcModalMachine.ts +++ b/screens/Home/MyVcs/AddVcModalMachine.ts @@ -330,11 +330,21 @@ export const AddVcModalMachine = 'OTP is invalid': 'invalidOtp', 'OTP has expired': 'expiredOtp', }; - return OTP_ERRORS_MAP[message] + + let otpErrorMessage = OTP_ERRORS_MAP[message] ? i18n.t(`errors.backend.${OTP_ERRORS_MAP[message]}`, { ns: 'AddVcModal', }) : message; + + sendErrorEvent( + getErrorEventData( + TelemetryConstants.FlowType.vcDownload, + message, + otpErrorMessage, + ), + ); + return otpErrorMessage; }, }), diff --git a/shared/storage.ts b/shared/storage.ts index 59fc911c21..293fadca7b 100644 --- a/shared/storage.ts +++ b/shared/storage.ts @@ -23,6 +23,8 @@ import { } from './constants'; import FileStorage, {getFilePath, vcDirectoryPath} from './fileStorage'; import {__AppId} from './GlobalVariables'; +import {getErrorEventData, sendErrorEvent} from './telemetry/TelemetryUtils'; +import {TelemetryConstants} from './telemetry/TelemetryConstants'; export const MMKV = new MMKVLoader().initialize(); @@ -80,6 +82,13 @@ class Storage { const isCorrupted = await this.isCorruptedVC(key, encryptionKey, data); if (isCorrupted) { + sendErrorEvent( + getErrorEventData( + TelemetryConstants.FlowType.fetchData, + TelemetryConstants.ErrorId.tampered, + 'VC is corrupted and will be deleted from storage', + ), + ); console.debug( '[Inji-406]: VC is corrupted and will be deleted from storage', ); @@ -110,9 +119,23 @@ class Storage { ); if (isDownloaded && error.message.includes(ENOENT)) { + sendErrorEvent( + getErrorEventData( + TelemetryConstants.FlowType.fetchData, + TelemetryConstants.ErrorId.dataRetrieval, + error.message, + ), + ); throw new Error(ENOENT); } } + sendErrorEvent( + getErrorEventData( + TelemetryConstants.FlowType.fetchData, + TelemetryConstants.ErrorId.dataRetrieval, + 'Error Occurred while retriving from Storage', + ), + ); console.log('Error Occurred while retriving from Storage.', error); throw error; diff --git a/shared/telemetry/TelemetryConstants.js b/shared/telemetry/TelemetryConstants.js index 25b99226b7..59b52e481c 100644 --- a/shared/telemetry/TelemetryConstants.js +++ b/shared/telemetry/TelemetryConstants.js @@ -10,6 +10,7 @@ export const TelemetryConstants = { appLogin: 'App Login', vcLockOrRevoke: 'VC Lock / VC Revoke', getVcUsingAid: 'Get VC using AID', + fetchData: 'Fetch Data', }), EndEventStatus: Object.freeze({ @@ -37,6 +38,8 @@ export const TelemetryConstants = { userCancel: 'USER_CANCEL', resend: 'RESEND', activationFailed: 'ACTIVATION_FAILED', + tampered: 'TAMPERED', + dataRetrieval: 'DATA_RETRIEVAL', }), Screens: Object.freeze({