Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/libs/Network/NetworkStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ let authTokenType: ValueOf<typeof CONST.AUTH_TOKEN_TYPES> | null;
let accountID: number | null | undefined;
let authenticating = false;

type AuthTokenDrop = {
source: 'setAuthToken' | 'session';
droppedAt: number;
stack?: string;
};

let lastAuthTokenDrop: AuthTokenDrop | undefined;

const AUTH_TOKEN_DROP_STACK_FRAMES = 8;

let resolveIsReadyPromise: (args?: unknown[]) => void;
let isReadyPromise = new Promise((resolve) => {
resolveIsReadyPromise = resolve;
Expand Down Expand Up @@ -44,7 +54,12 @@ function resetHasReadRequiredDataFromStorage() {
Onyx.connectWithoutView({
key: ONYXKEYS.SESSION,
callback: (val) => {
authToken = val?.authToken ?? null;
const newAuthToken = val?.authToken ?? null;
if (authToken && !newAuthToken) {
lastAuthTokenDrop = {source: 'session', droppedAt: Date.now()};
Log.info('[NetworkStore] authToken dropped', false, {source: 'session', hasEmail: !!val?.email, accountID: val?.accountID});
}
authToken = newAuthToken;
authTokenType = val?.authTokenType ?? null;
accountID = val?.accountID ?? null;
checkRequiredData();
Expand Down Expand Up @@ -86,9 +101,18 @@ function isSupportAuthToken(): boolean {
}

function setAuthToken(newAuthToken: string | null) {
if (authToken && !newAuthToken) {
const stack = new Error().stack?.split('\n').slice(0, AUTH_TOKEN_DROP_STACK_FRAMES).join('\n');
lastAuthTokenDrop = {source: 'setAuthToken', droppedAt: Date.now(), stack};
Log.info('[NetworkStore] authToken dropped', false, {source: 'setAuthToken', stack});
}
authToken = newAuthToken;
}

function getLastAuthTokenDrop(): AuthTokenDrop | undefined {
return lastAuthTokenDrop;
}

function hasReadRequiredDataFromStorage(): Promise<unknown> {
return isReadyPromise;
}
Expand All @@ -115,4 +139,5 @@ export {
isSupportAuthToken,
getLastShortAuthToken,
setLastShortAuthToken,
getLastAuthTokenDrop,
};
16 changes: 14 additions & 2 deletions src/libs/Network/enhanceParameters.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import {AUTHENTICATION_COMMAND} from '@libs/API/types';
import * as Environment from '@libs/Environment/Environment';
import getPlatform from '@libs/getPlatform';
import Log from '@libs/Log';

import CONFIG from '@src/CONFIG';
import ONYXKEYS from '@src/ONYXKEYS';

import Onyx from 'react-native-onyx';

import pkg from '../../../package.json';
import {getAuthToken, getCurrentUserEmail} from './NetworkStore';
import {getAuthToken, getCurrentUserEmail, getLastAuthTokenDrop} from './NetworkStore';

// For all requests, we'll send the lastUpdateID that is applied to this client. This will
// allow us to calculate previousUpdateID faster.
Expand Down Expand Up @@ -80,7 +81,18 @@ export default function enhanceParameters(command: string, parameters: Record<st
const finalParameters: Record<string, unknown> = {...parameters, ...getBaseRequestParameters(parameters.email)};

if (isAuthTokenRequired(command) && !parameters.authToken) {
finalParameters.authToken = getAuthToken() ?? null;
const authToken = getAuthToken();
if (!authToken) {
const lastDrop = getLastAuthTokenDrop();
Log.info('[enhanceParameters] Sending request without authToken', false, {
command,
authTokenState: authToken === undefined ? 'notHydrated' : 'empty',
lastDropSource: lastDrop?.source,
msSinceLastDrop: lastDrop ? Date.now() - lastDrop.droppedAt : undefined,
lastDropStack: lastDrop?.stack,
});
}
finalParameters.authToken = authToken ?? null;
}

finalParameters.clientUpdateID = lastUpdateIDAppliedToClient;
Expand Down
27 changes: 25 additions & 2 deletions src/libs/prepareRequestPayload/index.native.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
import {checkFileExistsWithReason} from '@libs/fileDownload/checkFileExists';
import {readFileAsync} from '@libs/fileDownload/FileUtils';
import getReceiptsUploadFolderPath from '@libs/getReceiptsUploadFolderPath';
import ReceiptStorage from '@libs/ReceiptStorage';
import {logReceiptDropped} from '@libs/telemetry/ReceiptObservability';
import validateFormDataParameter from '@libs/validateFormDataParameter';

import type {Receipt} from '@src/types/onyx/Transaction';

import RNFS from 'react-native-fs';

import type PrepareRequestPayload from './types';

async function getReceiptsFolderState(): Promise<{exists: boolean; entryCount?: number}> {
let folderPath: string;
try {
folderPath = getReceiptsUploadFolderPath();
if (!(await RNFS.exists(folderPath))) {
return {exists: false};
}
} catch {
return {exists: false};
}

try {
const entries = await RNFS.readDir(folderPath);
return {exists: true, entryCount: entries.length};
} catch {
return {exists: true};
}
}

/**
* Prepares the request payload (body) for a given command and data.
* This function is specifically designed for native platforms (IOS and Android) to handle the regeneration of blob files. It ensures that files, such as receipts, are properly read and appended to the FormData object before the request is sent.
Expand Down Expand Up @@ -38,8 +60,9 @@ const prepareRequestPayload: PrepareRequestPayload = (command, data, initiatedOf
return checkFileExistsWithReason(localUri).then(({exists, error}) => {
if (!exists) {
const transactionID = typeof data.transactionID === 'string' ? data.transactionID : undefined;
logReceiptDropped({receiptTraceId, transactionID, command, source, fileName: name, statError: error});
return;
return getReceiptsFolderState().then((receiptsFolder) => {
logReceiptDropped({receiptTraceId, transactionID, command, source, localUri, fileName: name, statError: error, receiptsFolder});
});
}
const receiptFormData = {
uri: localUri,
Expand Down
12 changes: 10 additions & 2 deletions src/libs/telemetry/ReceiptObservability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const RECEIPT_BEARING_COMMANDS = new Set<string>([
WRITE_COMMANDS.CATEGORIZE_TRACKED_EXPENSE,
WRITE_COMMANDS.SHARE_TRACKED_EXPENSE,
WRITE_COMMANDS.ADD_TRACKED_EXPENSE_TO_POLICY,
WRITE_COMMANDS.CREATE_DISTANCE_REQUEST,
]);

/** When each receipt was enqueued, keyed by transaction id, so a snapshot can report how long it has waited. */
Expand Down Expand Up @@ -178,33 +179,40 @@ function logReceiptEnqueued({receiptTraceId, transactionID, command, persistedQu
/**
* Records the dropped milestone: the receipt file was gone when we built the upload payload, so the request goes out
* without it. Logged at alert level on the [Receipt] spine so it reaches Sentry and joins the capture, submit, and
* enqueue lines by receiptTraceId. source and fileName are for the raw device log only; they are not whitelisted, so
* they never reach Sentry.
* enqueue lines by receiptTraceId. source, localUri and fileName are for the raw device log only; they are not
* whitelisted, so they never reach Sentry.
*/
function logReceiptDropped({
receiptTraceId,
transactionID,
command,
source,
localUri,
fileName,
statError,
receiptsFolder,
}: {
receiptTraceId: string | undefined;
transactionID: string | undefined;
command: string;
source: string | undefined;
localUri: string | undefined;
fileName: string | undefined;
statError: {message: string; code?: string} | undefined;
receiptsFolder: {exists: boolean; entryCount?: number};
}) {
Log.alert(`${RECEIPT_LOG_PREFIX} dropped`, {
event: 'dropped',
receiptTraceId,
transactionID,
command,
source,
localUri,
fileName,
statErrorCode: statError?.code,
statError: statError?.message,
receiptsFolderExists: String(receiptsFolder.exists),
receiptsFolderEntryCount: receiptsFolder.entryCount,
});
}

Expand Down
3 changes: 3 additions & 0 deletions src/libs/telemetry/forwardLogsToSentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ const PREFIX_SCOPED_PARAMETERS_WHITELIST = new Map<ForwardedLogPrefix, ReadonlyA
'isSourceInDurableFolder',
'msSinceEnqueued',
'pendingReceiptCount',
'isOffline',
'receiptsFolderExists',
'receiptsFolderEntryCount',
],
],
['[PDFStall]', ['reportID']],
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/libs/prepareRequestPayloadNativeTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@ describe('prepareRequestPayload (native)', () => {
transactionID: 'txn-456',
command: 'RequestMoney',
source: 'file:///var/mobile/Library/Caches/ImageManipulator/receipt.jpg',
localUri: 'file:///var/mobile/Library/Caches/ImageManipulator/receipt.jpg',
fileName: 'receipt.jpg',
// The errno separates a deleted file from one that is there but unreadable
statError: {message: 'ENOENT: no such file', code: 'ENOENT'},
receiptsFolder: {exists: false},
});
});

Expand Down
Loading