From 36a58b947a08ce655deec405b748634ce9ffdfab Mon Sep 17 00:00:00 2001 From: tienifr Date: Wed, 4 Oct 2023 10:58:24 +0700 Subject: [PATCH 1/4] fix: 28748 --- src/libs/Middleware/HandleUnusedOptimisticID.ts | 4 ++++ src/libs/Middleware/SaveResponseInOnyx.js | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/libs/Middleware/HandleUnusedOptimisticID.ts b/src/libs/Middleware/HandleUnusedOptimisticID.ts index a7e90accd3fc..a96eb4d5651b 100644 --- a/src/libs/Middleware/HandleUnusedOptimisticID.ts +++ b/src/libs/Middleware/HandleUnusedOptimisticID.ts @@ -10,6 +10,10 @@ const handleUnusedOptimisticID: Middleware = (requestResponse, request, isFromSe const responseOnyxData = response?.onyxData ?? []; responseOnyxData.forEach((onyxData) => { const key = onyxData.key; + if (!key) { + return; + } + if (!key.startsWith(ONYXKEYS.COLLECTION.REPORT)) { return; } diff --git a/src/libs/Middleware/SaveResponseInOnyx.js b/src/libs/Middleware/SaveResponseInOnyx.js index 8cb66c0c10d0..bea6fcaf7c93 100644 --- a/src/libs/Middleware/SaveResponseInOnyx.js +++ b/src/libs/Middleware/SaveResponseInOnyx.js @@ -57,7 +57,8 @@ function SaveResponseInOnyx(requestResponse, request) { ...response, shouldPauseQueue: true, }); - }); + // # TODO: update with correct logic to log error to the console + }).catch(error => console.log('error', error)); } export default SaveResponseInOnyx; From f201b448cb3abe02d5eba2bf7415d74ad41794c3 Mon Sep 17 00:00:00 2001 From: tienifr Date: Wed, 4 Oct 2023 18:15:29 +0700 Subject: [PATCH 2/4] catch error --- src/libs/Middleware/SaveResponseInOnyx.js | 78 ++++++++++++----------- 1 file changed, 40 insertions(+), 38 deletions(-) diff --git a/src/libs/Middleware/SaveResponseInOnyx.js b/src/libs/Middleware/SaveResponseInOnyx.js index bea6fcaf7c93..fa49ad5991a6 100644 --- a/src/libs/Middleware/SaveResponseInOnyx.js +++ b/src/libs/Middleware/SaveResponseInOnyx.js @@ -14,51 +14,53 @@ const requestsToIgnoreLastUpdateID = ['OpenApp', 'ReconnectApp', 'GetMissingOnyx * @returns {Promise} */ function SaveResponseInOnyx(requestResponse, request) { - return requestResponse.then((response) => { - // Make sure we have response data (i.e. response isn't a promise being passed down to us by a failed retry request and response undefined) - if (!response) { - return; - } - const onyxUpdates = response.onyxData; - - // Sometimes we call requests that are successfull but they don't have any response or any success/failure data to set. Let's return early since - // we don't need to store anything here. - if (!onyxUpdates && !request.successData && !request.failureData) { - return Promise.resolve(response); - } + return requestResponse + .then((response) => { + // Make sure we have response data (i.e. response isn't a promise being passed down to us by a failed retry request and response undefined) + if (!response) { + return; + } + const onyxUpdates = response.onyxData; - // If there is an OnyxUpdate for using memory only keys, enable them - _.find(onyxUpdates, ({key, value}) => { - if (key !== ONYXKEYS.IS_USING_MEMORY_ONLY_KEYS || !value) { - return false; + // Sometimes we call requests that are successfull but they don't have any response or any success/failure data to set. Let's return early since + // we don't need to store anything here. + if (!onyxUpdates && !request.successData && !request.failureData) { + return Promise.resolve(response); } - MemoryOnlyKeys.enable(); - return true; - }); + // If there is an OnyxUpdate for using memory only keys, enable them + _.find(onyxUpdates, ({key, value}) => { + if (key !== ONYXKEYS.IS_USING_MEMORY_ONLY_KEYS || !value) { + return false; + } - const responseToApply = { - type: CONST.ONYX_UPDATE_TYPES.HTTPS, - lastUpdateID: Number(response.lastUpdateID || 0), - previousUpdateID: Number(response.previousUpdateID || 0), - request, - response, - }; + MemoryOnlyKeys.enable(); + return true; + }); - if (_.includes(requestsToIgnoreLastUpdateID, request.command) || !OnyxUpdates.doesClientNeedToBeUpdated(Number(response.previousUpdateID || 0))) { - return OnyxUpdates.apply(responseToApply); - } + const responseToApply = { + type: CONST.ONYX_UPDATE_TYPES.HTTPS, + lastUpdateID: Number(response.lastUpdateID || 0), + previousUpdateID: Number(response.previousUpdateID || 0), + request, + response, + }; + + if (_.includes(requestsToIgnoreLastUpdateID, request.command) || !OnyxUpdates.doesClientNeedToBeUpdated(Number(response.previousUpdateID || 0))) { + return OnyxUpdates.apply(responseToApply); + } - // Save the update IDs to Onyx so they can be used to fetch incremental updates if the client gets out of sync from the server - OnyxUpdates.saveUpdateInformation(responseToApply); + // Save the update IDs to Onyx so they can be used to fetch incremental updates if the client gets out of sync from the server + OnyxUpdates.saveUpdateInformation(responseToApply); - // Ensure the queue is paused while the client resolves the gap in onyx updates so that updates are guaranteed to happen in a specific order. - return Promise.resolve({ - ...response, - shouldPauseQueue: true, - }); - // # TODO: update with correct logic to log error to the console - }).catch(error => console.log('error', error)); + // Ensure the queue is paused while the client resolves the gap in onyx updates so that updates are guaranteed to happen in a specific order. + return Promise.resolve({ + ...response, + shouldPauseQueue: true, + }); + // # TODO: update with correct logic to log error to the console + }) + .catch(console.error); } export default SaveResponseInOnyx; From b92d2bda36575d01450cfa81f0593d21b7dc2769 Mon Sep 17 00:00:00 2001 From: tienifr Date: Wed, 4 Oct 2023 18:17:13 +0700 Subject: [PATCH 3/4] run prettier --- src/libs/Middleware/SaveResponseInOnyx.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libs/Middleware/SaveResponseInOnyx.js b/src/libs/Middleware/SaveResponseInOnyx.js index fa49ad5991a6..cb55f50bc04d 100644 --- a/src/libs/Middleware/SaveResponseInOnyx.js +++ b/src/libs/Middleware/SaveResponseInOnyx.js @@ -58,7 +58,6 @@ function SaveResponseInOnyx(requestResponse, request) { ...response, shouldPauseQueue: true, }); - // # TODO: update with correct logic to log error to the console }) .catch(console.error); } From 8410b966258cbf7dce4d2d7e39891978620c73a6 Mon Sep 17 00:00:00 2001 From: tienifr Date: Wed, 4 Oct 2023 19:13:58 +0700 Subject: [PATCH 4/4] detailed error message --- src/libs/Middleware/SaveResponseInOnyx.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libs/Middleware/SaveResponseInOnyx.js b/src/libs/Middleware/SaveResponseInOnyx.js index cb55f50bc04d..5c096bf3da72 100644 --- a/src/libs/Middleware/SaveResponseInOnyx.js +++ b/src/libs/Middleware/SaveResponseInOnyx.js @@ -59,7 +59,9 @@ function SaveResponseInOnyx(requestResponse, request) { shouldPauseQueue: true, }); }) - .catch(console.error); + .catch((err) => { + console.error('Got exception while saving response in Onyx', err); + }); } export default SaveResponseInOnyx;