Skip to content

Commit 2cd2ef0

Browse files
sergey-akhalkovpatniko
authored andcommitted
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
1 parent 5ece19b commit 2cd2ef0

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

CodePush.js

+24-3
Original file line numberDiff line numberDiff line change
@@ -238,15 +238,36 @@ const sync = (() => {
238238
const setSyncCompleted = () => { syncInProgress = false; };
239239

240240
return (options = {}, syncStatusChangeCallback, downloadProgressCallback) => {
241+
let syncStatusCallbackWithTryCatch, downloadProgressCallbackkWithTryCatch;
242+
if (typeof syncStatusChangeCallback === "function") {
243+
syncStatusCallbackWithTryCatch = (...args) => {
244+
try {
245+
syncStatusChangeCallback(...args);
246+
} catch (error) {
247+
log(`An error has occurred : ${error.stack}`);
248+
}
249+
}
250+
}
251+
252+
if (typeof downloadProgressCallback === "function") {
253+
downloadProgressCallbackkWithTryCatch = (...args) => {
254+
try {
255+
downloadProgressCallback(...args);
256+
} catch (error) {
257+
log(`An error has occurred: ${error.stack}`);
258+
}
259+
}
260+
}
261+
241262
if (syncInProgress) {
242-
typeof syncStatusChangeCallback === "function"
243-
? syncStatusChangeCallback(CodePush.SyncStatus.SYNC_IN_PROGRESS)
263+
typeof syncStatusCallbackWithTryCatch === "function"
264+
? syncStatusCallbackWithTryCatch(CodePush.SyncStatus.SYNC_IN_PROGRESS)
244265
: log("Sync already in progress.");
245266
return Promise.resolve(CodePush.SyncStatus.SYNC_IN_PROGRESS);
246267
}
247268

248269
syncInProgress = true;
249-
const syncPromise = syncInternal(options, syncStatusChangeCallback, downloadProgressCallback);
270+
const syncPromise = syncInternal(options, syncStatusCallbackWithTryCatch, downloadProgressCallbackkWithTryCatch);
250271
syncPromise
251272
.then(setSyncCompleted)
252273
.catch(setSyncCompleted);

0 commit comments

Comments
 (0)