Skip to content

Commit

Permalink
Merge pull request #30832 from DylanDylann/fix/24141
Browse files Browse the repository at this point in the history
Fix/24141: Add error handling to readFilyAsync
  • Loading branch information
luacmartins authored Nov 9, 2023
2 parents 5c1ba6e + 3296817 commit 5cff53b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
9 changes: 6 additions & 3 deletions src/libs/fileDownload/FileUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,12 @@ function appendTimeToFileName(fileName) {
*
* @param {String} path - the blob url of the locally uplodaded file
* @param {String} fileName
* @param {Function} onSuccess
* @param {Function} onFailure
*
* @returns {Promise}
*/
const readFileAsync = (path, fileName) =>
const readFileAsync = (path, fileName, onSuccess, onFailure = () => {}) =>
new Promise((resolve) => {
if (!path) {
resolve();
Expand All @@ -183,11 +186,11 @@ const readFileAsync = (path, fileName) =>
// For some reason, the File object on iOS does not have a uri property
// so images aren't uploaded correctly to the backend
file.uri = path;
resolve(file);
onSuccess(file);
})
.catch((e) => {
console.debug('[FileUtils] Could not read uploaded file', e);
resolve();
onFailure(e);
});
});

Expand Down
9 changes: 5 additions & 4 deletions src/pages/iou/ReceiptSelector/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,12 @@ function ReceiptSelector({route, report, iou, transactionID, isInTabNavigator, s
const filePath = `file://${photo.path}`;
IOU.setMoneyRequestReceipt(filePath, photo.path);

if (transactionID) {
FileUtils.readFileAsync(filePath, photo.path).then((receipt) => {
IOU.replaceReceipt(transactionID, receipt, filePath);
});
const onSuccess = (receipt) => {
IOU.replaceReceipt(transactionID, receipt, filePath);
};

if (transactionID) {
FileUtils.readFileAsync(filePath, photo.path, onSuccess);
Navigation.dismissModal();
return;
}
Expand Down
23 changes: 12 additions & 11 deletions src/pages/iou/steps/MoneyRequestConfirmPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ function MoneyRequestConfirmPage(props) {
if (!props.iou.receiptPath || !props.iou.receiptFilename) {
return;
}
FileUtils.readFileAsync(props.iou.receiptPath, props.iou.receiptFilename).then((file) => {
if (!file) {
Navigation.goBack(ROUTES.MONEY_REQUEST.getRoute(iouType, reportID));
} else {
const receipt = file;
receipt.state = file && isManualRequestDM ? CONST.IOU.RECEIPT_STATE.OPEN : CONST.IOU.RECEIPT_STATE.SCANREADY;
setReceiptFile(receipt);
}
});
const onSuccess = (file) => {
const receipt = file;
receipt.state = file && isManualRequestDM ? CONST.IOU.RECEIPT_STATE.OPEN : CONST.IOU.RECEIPT_STATE.SCANREADY;
setReceiptFile(receipt);
};
const onFailure = () => {
Navigation.goBack(ROUTES.MONEY_REQUEST.getRoute(iouType, reportID));
};
FileUtils.readFileAsync(props.iou.receiptPath, props.iou.receiptFilename, onSuccess, onFailure);
}, [props.iou.receiptPath, props.iou.receiptFilename, isManualRequestDM, iouType, reportID]);

useEffect(() => {
Expand Down Expand Up @@ -217,7 +217,7 @@ function MoneyRequestConfirmPage(props) {
// If we have a receipt let's start the split bill by creating only the action, the transaction, and the group DM if needed
if (iouType === CONST.IOU.TYPE.SPLIT && props.iou.receiptPath) {
const existingSplitChatReportID = CONST.REGEX.NUMBER.test(reportID) ? reportID : '';
FileUtils.readFileAsync(props.iou.receiptPath, props.iou.receiptFilename).then((receipt) => {
const onSuccess = (receipt) => {
IOU.startSplitBill(
selectedParticipants,
props.currentUserPersonalDetails.login,
Expand All @@ -226,7 +226,8 @@ function MoneyRequestConfirmPage(props) {
receipt,
existingSplitChatReportID,
);
});
};
FileUtils.readFileAsync(props.iou.receiptPath, props.iou.receiptFilename, onSuccess);
return;
}

Expand Down

0 comments on commit 5cff53b

Please sign in to comment.