From 2cd2ef0ca2e27a95f84579603c2d222188bb9ce5 Mon Sep 17 00:00:00 2001 From: Sergey Akhalkov Date: Thu, 27 Apr 2017 23:34:44 +0300 Subject: [PATCH] sync: install update even if an error occurs in callbacks (#787) Wrap up syncStatusChangeCallback and downloadProgressCallback methods with try/catch block to make it possible to install CodePush updates even if an error occurs while trying to run syncStatusChangeCallback or downloadProgressCallback methods --- CodePush.js | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/CodePush.js b/CodePush.js index 3c74dca..bbd163d 100644 --- a/CodePush.js +++ b/CodePush.js @@ -238,15 +238,36 @@ const sync = (() => { const setSyncCompleted = () => { syncInProgress = false; }; return (options = {}, syncStatusChangeCallback, downloadProgressCallback) => { + let syncStatusCallbackWithTryCatch, downloadProgressCallbackkWithTryCatch; + if (typeof syncStatusChangeCallback === "function") { + syncStatusCallbackWithTryCatch = (...args) => { + try { + syncStatusChangeCallback(...args); + } catch (error) { + log(`An error has occurred : ${error.stack}`); + } + } + } + + if (typeof downloadProgressCallback === "function") { + downloadProgressCallbackkWithTryCatch = (...args) => { + try { + downloadProgressCallback(...args); + } catch (error) { + log(`An error has occurred: ${error.stack}`); + } + } + } + if (syncInProgress) { - typeof syncStatusChangeCallback === "function" - ? syncStatusChangeCallback(CodePush.SyncStatus.SYNC_IN_PROGRESS) + typeof syncStatusCallbackWithTryCatch === "function" + ? syncStatusCallbackWithTryCatch(CodePush.SyncStatus.SYNC_IN_PROGRESS) : log("Sync already in progress."); return Promise.resolve(CodePush.SyncStatus.SYNC_IN_PROGRESS); } syncInProgress = true; - const syncPromise = syncInternal(options, syncStatusChangeCallback, downloadProgressCallback); + const syncPromise = syncInternal(options, syncStatusCallbackWithTryCatch, downloadProgressCallbackkWithTryCatch); syncPromise .then(setSyncCompleted) .catch(setSyncCompleted);