From 07ef7d84279cd4e0dcac47c6a4d9ec6c6946da4f Mon Sep 17 00:00:00 2001 From: "alexander.goncharov" Date: Mon, 21 May 2018 11:42:54 +0300 Subject: [PATCH 1/4] Convert all @ReactMethod in try-catch block --- .../codepush/react/CodePushNativeModule.java | 379 ++++++++++-------- 1 file changed, 217 insertions(+), 162 deletions(-) diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java index 9a20ee7b2..61ba5b48d 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java @@ -262,6 +262,9 @@ public void dispatchDownloadProgressEvent() { e.printStackTrace(); mSettingsManager.saveFailedUpdate(CodePushUtils.convertReadableToJsonObject(updatePackage)); promise.reject(e); + } catch (Exception e) { + e.printStackTrace(); + promise.reject(e); } return null; @@ -273,18 +276,23 @@ public void dispatchDownloadProgressEvent() { @ReactMethod public void getConfiguration(Promise promise) { - WritableMap configMap = Arguments.createMap(); - configMap.putString("appVersion", mCodePush.getAppVersion()); - configMap.putString("clientUniqueId", mClientUniqueId); - configMap.putString("deploymentKey", mCodePush.getDeploymentKey()); - configMap.putString("serverUrl", mCodePush.getServerUrl()); - - // The binary hash may be null in debug builds - if (mBinaryContentsHash != null) { - configMap.putString(CodePushConstants.PACKAGE_HASH_KEY, mBinaryContentsHash); - } + try { + WritableMap configMap = Arguments.createMap(); + configMap.putString("appVersion", mCodePush.getAppVersion()); + configMap.putString("clientUniqueId", mClientUniqueId); + configMap.putString("deploymentKey", mCodePush.getDeploymentKey()); + configMap.putString("serverUrl", mCodePush.getServerUrl()); + + // The binary hash may be null in debug builds + if (mBinaryContentsHash != null) { + configMap.putString(CodePushConstants.PACKAGE_HASH_KEY, mBinaryContentsHash); + } - promise.resolve(configMap); + promise.resolve(configMap); + } catch(Exception e) { + e.printStackTrace(); + promise.reject(e); + } } @ReactMethod @@ -292,50 +300,55 @@ public void getUpdateMetadata(final int updateState, final Promise promise) { AsyncTask asyncTask = new AsyncTask() { @Override protected Void doInBackground(Void... params) { - JSONObject currentPackage = mUpdateManager.getCurrentPackage(); - - if (currentPackage == null) { - promise.resolve(null); - return null; - } - - Boolean currentUpdateIsPending = false; - - if (currentPackage.has(CodePushConstants.PACKAGE_HASH_KEY)) { - String currentHash = currentPackage.optString(CodePushConstants.PACKAGE_HASH_KEY, null); - currentUpdateIsPending = mSettingsManager.isPendingUpdate(currentHash); - } - - if (updateState == CodePushUpdateState.PENDING.getValue() && !currentUpdateIsPending) { - // The caller wanted a pending update - // but there isn't currently one. - promise.resolve(null); - } else if (updateState == CodePushUpdateState.RUNNING.getValue() && currentUpdateIsPending) { - // The caller wants the running update, but the current - // one is pending, so we need to grab the previous. - JSONObject previousPackage = mUpdateManager.getPreviousPackage(); + try { + JSONObject currentPackage = mUpdateManager.getCurrentPackage(); - if (previousPackage == null) { + if (currentPackage == null) { promise.resolve(null); return null; } - promise.resolve(CodePushUtils.convertJsonObjectToWritable(previousPackage)); - } else { - // The current package satisfies the request: - // 1) Caller wanted a pending, and there is a pending update - // 2) Caller wanted the running update, and there isn't a pending - // 3) Caller wants the latest update, regardless if it's pending or not - if (mCodePush.isRunningBinaryVersion()) { - // This only matters in Debug builds. Since we do not clear "outdated" updates, - // we need to indicate to the JS side that somehow we have a current update on - // disk that is not actually running. - CodePushUtils.setJSONValueForKey(currentPackage, "_isDebugOnly", true); + Boolean currentUpdateIsPending = false; + + if (currentPackage.has(CodePushConstants.PACKAGE_HASH_KEY)) { + String currentHash = currentPackage.optString(CodePushConstants.PACKAGE_HASH_KEY, null); + currentUpdateIsPending = mSettingsManager.isPendingUpdate(currentHash); } - // Enable differentiating pending vs. non-pending updates - CodePushUtils.setJSONValueForKey(currentPackage, "isPending", currentUpdateIsPending); - promise.resolve(CodePushUtils.convertJsonObjectToWritable(currentPackage)); + if (updateState == CodePushUpdateState.PENDING.getValue() && !currentUpdateIsPending) { + // The caller wanted a pending update + // but there isn't currently one. + promise.resolve(null); + } else if (updateState == CodePushUpdateState.RUNNING.getValue() && currentUpdateIsPending) { + // The caller wants the running update, but the current + // one is pending, so we need to grab the previous. + JSONObject previousPackage = mUpdateManager.getPreviousPackage(); + + if (previousPackage == null) { + promise.resolve(null); + return null; + } + + promise.resolve(CodePushUtils.convertJsonObjectToWritable(previousPackage)); + } else { + // The current package satisfies the request: + // 1) Caller wanted a pending, and there is a pending update + // 2) Caller wanted the running update, and there isn't a pending + // 3) Caller wants the latest update, regardless if it's pending or not + if (mCodePush.isRunningBinaryVersion()) { + // This only matters in Debug builds. Since we do not clear "outdated" updates, + // we need to indicate to the JS side that somehow we have a current update on + // disk that is not actually running. + CodePushUtils.setJSONValueForKey(currentPackage, "_isDebugOnly", true); + } + + // Enable differentiating pending vs. non-pending updates + CodePushUtils.setJSONValueForKey(currentPackage, "isPending", currentUpdateIsPending); + promise.resolve(CodePushUtils.convertJsonObjectToWritable(currentPackage)); + } + } catch(Exception e) { + e.printStackTrace(); + promise.reject(e); } return null; @@ -350,46 +363,51 @@ public void getNewStatusReport(final Promise promise) { AsyncTask asyncTask = new AsyncTask() { @Override protected Void doInBackground(Void... params) { - if (mCodePush.needToReportRollback()) { - mCodePush.setNeedToReportRollback(false); - JSONArray failedUpdates = mSettingsManager.getFailedUpdates(); - if (failedUpdates != null && failedUpdates.length() > 0) { - try { - JSONObject lastFailedPackageJSON = failedUpdates.getJSONObject(failedUpdates.length() - 1); - WritableMap lastFailedPackage = CodePushUtils.convertJsonObjectToWritable(lastFailedPackageJSON); - WritableMap failedStatusReport = mTelemetryManager.getRollbackReport(lastFailedPackage); - if (failedStatusReport != null) { - promise.resolve(failedStatusReport); + try { + if (mCodePush.needToReportRollback()) { + mCodePush.setNeedToReportRollback(false); + JSONArray failedUpdates = mSettingsManager.getFailedUpdates(); + if (failedUpdates != null && failedUpdates.length() > 0) { + try { + JSONObject lastFailedPackageJSON = failedUpdates.getJSONObject(failedUpdates.length() - 1); + WritableMap lastFailedPackage = CodePushUtils.convertJsonObjectToWritable(lastFailedPackageJSON); + WritableMap failedStatusReport = mTelemetryManager.getRollbackReport(lastFailedPackage); + if (failedStatusReport != null) { + promise.resolve(failedStatusReport); + return null; + } + } catch (JSONException e) { + throw new CodePushUnknownException("Unable to read failed updates information stored in SharedPreferences.", e); + } + } + } else if (mCodePush.didUpdate()) { + JSONObject currentPackage = mUpdateManager.getCurrentPackage(); + if (currentPackage != null) { + WritableMap newPackageStatusReport = mTelemetryManager.getUpdateReport(CodePushUtils.convertJsonObjectToWritable(currentPackage)); + if (newPackageStatusReport != null) { + promise.resolve(newPackageStatusReport); return null; } - } catch (JSONException e) { - throw new CodePushUnknownException("Unable to read failed updates information stored in SharedPreferences.", e); } - } - } else if (mCodePush.didUpdate()) { - JSONObject currentPackage = mUpdateManager.getCurrentPackage(); - if (currentPackage != null) { - WritableMap newPackageStatusReport = mTelemetryManager.getUpdateReport(CodePushUtils.convertJsonObjectToWritable(currentPackage)); - if (newPackageStatusReport != null) { - promise.resolve(newPackageStatusReport); + } else if (mCodePush.isRunningBinaryVersion()) { + WritableMap newAppVersionStatusReport = mTelemetryManager.getBinaryUpdateReport(mCodePush.getAppVersion()); + if (newAppVersionStatusReport != null) { + promise.resolve(newAppVersionStatusReport); + return null; + } + } else { + WritableMap retryStatusReport = mTelemetryManager.getRetryStatusReport(); + if (retryStatusReport != null) { + promise.resolve(retryStatusReport); return null; } } - } else if (mCodePush.isRunningBinaryVersion()) { - WritableMap newAppVersionStatusReport = mTelemetryManager.getBinaryUpdateReport(mCodePush.getAppVersion()); - if (newAppVersionStatusReport != null) { - promise.resolve(newAppVersionStatusReport); - return null; - } - } else { - WritableMap retryStatusReport = mTelemetryManager.getRetryStatusReport(); - if (retryStatusReport != null) { - promise.resolve(retryStatusReport); - return null; - } - } - promise.resolve(""); + promise.resolve(""); + } catch(Exception e) { + e.printStackTrace(); + promise.reject(e); + } return null; } }; @@ -402,76 +420,81 @@ public void installUpdate(final ReadableMap updatePackage, final int installMode AsyncTask asyncTask = new AsyncTask() { @Override protected Void doInBackground(Void... params) { - mUpdateManager.installPackage(CodePushUtils.convertReadableToJsonObject(updatePackage), mSettingsManager.isPendingUpdate(null)); - - String pendingHash = CodePushUtils.tryGetString(updatePackage, CodePushConstants.PACKAGE_HASH_KEY); - if (pendingHash == null) { - throw new CodePushUnknownException("Update package to be installed has no hash."); - } else { - mSettingsManager.savePendingUpdate(pendingHash, /* isLoading */false); - } + try { + mUpdateManager.installPackage(CodePushUtils.convertReadableToJsonObject(updatePackage), mSettingsManager.isPendingUpdate(null)); - if (installMode == CodePushInstallMode.ON_NEXT_RESUME.getValue() || - // We also add the resume listener if the installMode is IMMEDIATE, because - // if the current activity is backgrounded, we want to reload the bundle when - // it comes back into the foreground. - installMode == CodePushInstallMode.IMMEDIATE.getValue() || - installMode == CodePushInstallMode.ON_NEXT_SUSPEND.getValue()) { - - // Store the minimum duration on the native module as an instance - // variable instead of relying on a closure below, so that any - // subsequent resume-based installs could override it. - CodePushNativeModule.this.mMinimumBackgroundDuration = minimumBackgroundDuration; - - if (mLifecycleEventListener == null) { - // Ensure we do not add the listener twice. - mLifecycleEventListener = new LifecycleEventListener() { - private Date lastPausedDate = null; - private Handler appSuspendHandler = new Handler(Looper.getMainLooper()); - private Runnable loadBundleRunnable = new Runnable() { - @Override - public void run() { - CodePushUtils.log("Loading bundle on suspend"); - loadBundle(); - } - }; + String pendingHash = CodePushUtils.tryGetString(updatePackage, CodePushConstants.PACKAGE_HASH_KEY); + if (pendingHash == null) { + throw new CodePushUnknownException("Update package to be installed has no hash."); + } else { + mSettingsManager.savePendingUpdate(pendingHash, /* isLoading */false); + } - @Override - public void onHostResume() { - appSuspendHandler.removeCallbacks(loadBundleRunnable); - // As of RN 36, the resume handler fires immediately if the app is in - // the foreground, so explicitly wait for it to be backgrounded first - if (lastPausedDate != null) { - long durationInBackground = (new Date().getTime() - lastPausedDate.getTime()) / 1000; - if (installMode == CodePushInstallMode.IMMEDIATE.getValue() - || durationInBackground >= CodePushNativeModule.this.mMinimumBackgroundDuration) { - CodePushUtils.log("Loading bundle on resume"); + if (installMode == CodePushInstallMode.ON_NEXT_RESUME.getValue() || + // We also add the resume listener if the installMode is IMMEDIATE, because + // if the current activity is backgrounded, we want to reload the bundle when + // it comes back into the foreground. + installMode == CodePushInstallMode.IMMEDIATE.getValue() || + installMode == CodePushInstallMode.ON_NEXT_SUSPEND.getValue()) { + + // Store the minimum duration on the native module as an instance + // variable instead of relying on a closure below, so that any + // subsequent resume-based installs could override it. + CodePushNativeModule.this.mMinimumBackgroundDuration = minimumBackgroundDuration; + + if (mLifecycleEventListener == null) { + // Ensure we do not add the listener twice. + mLifecycleEventListener = new LifecycleEventListener() { + private Date lastPausedDate = null; + private Handler appSuspendHandler = new Handler(Looper.getMainLooper()); + private Runnable loadBundleRunnable = new Runnable() { + @Override + public void run() { + CodePushUtils.log("Loading bundle on suspend"); loadBundle(); } + }; + + @Override + public void onHostResume() { + appSuspendHandler.removeCallbacks(loadBundleRunnable); + // As of RN 36, the resume handler fires immediately if the app is in + // the foreground, so explicitly wait for it to be backgrounded first + if (lastPausedDate != null) { + long durationInBackground = (new Date().getTime() - lastPausedDate.getTime()) / 1000; + if (installMode == CodePushInstallMode.IMMEDIATE.getValue() + || durationInBackground >= CodePushNativeModule.this.mMinimumBackgroundDuration) { + CodePushUtils.log("Loading bundle on resume"); + loadBundle(); + } + } } - } - @Override - public void onHostPause() { - // Save the current time so that when the app is later - // resumed, we can detect how long it was in the background. - lastPausedDate = new Date(); + @Override + public void onHostPause() { + // Save the current time so that when the app is later + // resumed, we can detect how long it was in the background. + lastPausedDate = new Date(); - if (installMode == CodePushInstallMode.ON_NEXT_SUSPEND.getValue() && mSettingsManager.isPendingUpdate(null)) { - appSuspendHandler.postDelayed(loadBundleRunnable, minimumBackgroundDuration * 1000); + if (installMode == CodePushInstallMode.ON_NEXT_SUSPEND.getValue() && mSettingsManager.isPendingUpdate(null)) { + appSuspendHandler.postDelayed(loadBundleRunnable, minimumBackgroundDuration * 1000); + } } - } - @Override - public void onHostDestroy() { - } - }; + @Override + public void onHostDestroy() { + } + }; - getReactApplicationContext().addLifecycleEventListener(mLifecycleEventListener); + getReactApplicationContext().addLifecycleEventListener(mLifecycleEventListener); + } } - } - promise.resolve(""); + promise.resolve(""); + } catch(Exception e) { + e.printStackTrace(); + promise.reject(e); + } return null; } @@ -482,57 +505,89 @@ public void onHostDestroy() { @ReactMethod public void isFailedUpdate(String packageHash, Promise promise) { - promise.resolve(mSettingsManager.isFailedHash(packageHash)); + try { + promise.resolve(mSettingsManager.isFailedHash(packageHash)); + } catch(Exception e) { + e.printStackTrace(); + promise.reject(e); + } } @ReactMethod public void isFirstRun(String packageHash, Promise promise) { - boolean isFirstRun = mCodePush.didUpdate() - && packageHash != null - && packageHash.length() > 0 - && packageHash.equals(mUpdateManager.getCurrentPackageHash()); - promise.resolve(isFirstRun); + try { + boolean isFirstRun = mCodePush.didUpdate() + && packageHash != null + && packageHash.length() > 0 + && packageHash.equals(mUpdateManager.getCurrentPackageHash()); + promise.resolve(isFirstRun); + } catch(Exception e) { + e.printStackTrace(); + promise.reject(e); + } } @ReactMethod public void notifyApplicationReady(Promise promise) { - mSettingsManager.removePendingUpdate(); - promise.resolve(""); + try { + mSettingsManager.removePendingUpdate(); + promise.resolve(""); + } catch(Exception e) { + e.printStackTrace(); + promise.reject(e); + } } @ReactMethod public void recordStatusReported(ReadableMap statusReport) { - mTelemetryManager.recordStatusReported(statusReport); + try { + mTelemetryManager.recordStatusReported(statusReport); + } catch(Exception e) { + e.printStackTrace(); + } } @ReactMethod public void restartApp(boolean onlyIfUpdateIsPending, Promise promise) { - // If this is an unconditional restart request, or there - // is current pending update, then reload the app. - if (!onlyIfUpdateIsPending || mSettingsManager.isPendingUpdate(null)) { - loadBundle(); - promise.resolve(true); - return; - } + try { + // If this is an unconditional restart request, or there + // is current pending update, then reload the app. + if (!onlyIfUpdateIsPending || mSettingsManager.isPendingUpdate(null)) { + loadBundle(); + promise.resolve(true); + return; + } - promise.resolve(false); + promise.resolve(false); + } catch(Exception e) { + e.printStackTrace(); + promise.reject(e); + } } @ReactMethod public void saveStatusReportForRetry(ReadableMap statusReport) { - mTelemetryManager.saveStatusReportForRetry(statusReport); + try { + mTelemetryManager.saveStatusReportForRetry(statusReport); + } catch(Exception e) { + e.printStackTrace(); + } } @ReactMethod // Replaces the current bundle with the one downloaded from removeBundleUrl. // It is only to be used during tests. No-ops if the test configuration flag is not set. public void downloadAndReplaceCurrentBundle(String remoteBundleUrl) { - if (mCodePush.isUsingTestConfiguration()) { - try { - mUpdateManager.downloadAndReplaceCurrentBundle(remoteBundleUrl, mCodePush.getAssetsBundleFileName()); - } catch (IOException e) { - throw new CodePushUnknownException("Unable to replace current bundle", e); + try { + if (mCodePush.isUsingTestConfiguration()) { + try { + mUpdateManager.downloadAndReplaceCurrentBundle(remoteBundleUrl, mCodePush.getAssetsBundleFileName()); + } catch (IOException e) { + throw new CodePushUnknownException("Unable to replace current bundle", e); + } } + } catch(Exception e) { + e.printStackTrace(); } } } From 376b0531cb73e50d7f73758ef2ed414ab99aaaa1 Mon Sep 17 00:00:00 2001 From: "alexander.goncharov" Date: Mon, 21 May 2018 18:47:00 +0300 Subject: [PATCH 2/4] Fix Exception type --- .../codepush/react/CodePushNativeModule.java | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java index 61ba5b48d..d707d8d96 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java @@ -262,7 +262,7 @@ public void dispatchDownloadProgressEvent() { e.printStackTrace(); mSettingsManager.saveFailedUpdate(CodePushUtils.convertReadableToJsonObject(updatePackage)); promise.reject(e); - } catch (Exception e) { + } catch (CodePushUnknownException e) { e.printStackTrace(); promise.reject(e); } @@ -289,7 +289,7 @@ public void getConfiguration(Promise promise) { } promise.resolve(configMap); - } catch(Exception e) { + } catch(CodePushUnknownException e) { e.printStackTrace(); promise.reject(e); } @@ -346,7 +346,7 @@ protected Void doInBackground(Void... params) { CodePushUtils.setJSONValueForKey(currentPackage, "isPending", currentUpdateIsPending); promise.resolve(CodePushUtils.convertJsonObjectToWritable(currentPackage)); } - } catch(Exception e) { + } catch(CodePushUnknownException e) { e.printStackTrace(); promise.reject(e); } @@ -404,7 +404,7 @@ protected Void doInBackground(Void... params) { } promise.resolve(""); - } catch(Exception e) { + } catch(CodePushUnknownException e) { e.printStackTrace(); promise.reject(e); } @@ -491,7 +491,7 @@ public void onHostDestroy() { } promise.resolve(""); - } catch(Exception e) { + } catch(CodePushUnknownException e) { e.printStackTrace(); promise.reject(e); } @@ -507,7 +507,7 @@ public void onHostDestroy() { public void isFailedUpdate(String packageHash, Promise promise) { try { promise.resolve(mSettingsManager.isFailedHash(packageHash)); - } catch(Exception e) { + } catch(CodePushUnknownException e) { e.printStackTrace(); promise.reject(e); } @@ -521,7 +521,7 @@ public void isFirstRun(String packageHash, Promise promise) { && packageHash.length() > 0 && packageHash.equals(mUpdateManager.getCurrentPackageHash()); promise.resolve(isFirstRun); - } catch(Exception e) { + } catch(CodePushUnknownException e) { e.printStackTrace(); promise.reject(e); } @@ -532,7 +532,7 @@ public void notifyApplicationReady(Promise promise) { try { mSettingsManager.removePendingUpdate(); promise.resolve(""); - } catch(Exception e) { + } catch(CodePushUnknownException e) { e.printStackTrace(); promise.reject(e); } @@ -542,7 +542,7 @@ public void notifyApplicationReady(Promise promise) { public void recordStatusReported(ReadableMap statusReport) { try { mTelemetryManager.recordStatusReported(statusReport); - } catch(Exception e) { + } catch(CodePushUnknownException e) { e.printStackTrace(); } } @@ -559,7 +559,7 @@ public void restartApp(boolean onlyIfUpdateIsPending, Promise promise) { } promise.resolve(false); - } catch(Exception e) { + } catch(CodePushUnknownException e) { e.printStackTrace(); promise.reject(e); } @@ -569,7 +569,7 @@ public void restartApp(boolean onlyIfUpdateIsPending, Promise promise) { public void saveStatusReportForRetry(ReadableMap statusReport) { try { mTelemetryManager.saveStatusReportForRetry(statusReport); - } catch(Exception e) { + } catch(CodePushUnknownException e) { e.printStackTrace(); } } @@ -586,7 +586,9 @@ public void downloadAndReplaceCurrentBundle(String remoteBundleUrl) { throw new CodePushUnknownException("Unable to replace current bundle", e); } } - } catch(Exception e) { + } catch(CodePushUnknownException e) { + e.printStackTrace(); + } catch(CodePushMalformedDataException e) { e.printStackTrace(); } } From 725592cfd7e90b8feb3ab4e315dff29f189417f7 Mon Sep 17 00:00:00 2001 From: Alexander Goncharov Date: Thu, 24 May 2018 14:03:02 +0300 Subject: [PATCH 3/4] Replaced e.printStackTrace() with CodePushUtils.log(e) --- .../codepush/react/CodePushNativeModule.java | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java index d707d8d96..8ba839b1b 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java @@ -256,14 +256,14 @@ public void dispatchDownloadProgressEvent() { JSONObject newPackage = mUpdateManager.getPackage(CodePushUtils.tryGetString(updatePackage, CodePushConstants.PACKAGE_HASH_KEY)); promise.resolve(CodePushUtils.convertJsonObjectToWritable(newPackage)); } catch (IOException e) { - e.printStackTrace(); + CodePushUtils.log(e); promise.reject(e); } catch (CodePushInvalidUpdateException e) { - e.printStackTrace(); + CodePushUtils.log(e); mSettingsManager.saveFailedUpdate(CodePushUtils.convertReadableToJsonObject(updatePackage)); promise.reject(e); } catch (CodePushUnknownException e) { - e.printStackTrace(); + CodePushUtils.log(e); promise.reject(e); } @@ -290,7 +290,7 @@ public void getConfiguration(Promise promise) { promise.resolve(configMap); } catch(CodePushUnknownException e) { - e.printStackTrace(); + CodePushUtils.log(e); promise.reject(e); } } @@ -347,7 +347,7 @@ protected Void doInBackground(Void... params) { promise.resolve(CodePushUtils.convertJsonObjectToWritable(currentPackage)); } } catch(CodePushUnknownException e) { - e.printStackTrace(); + CodePushUtils.log(e); promise.reject(e); } @@ -405,7 +405,7 @@ protected Void doInBackground(Void... params) { promise.resolve(""); } catch(CodePushUnknownException e) { - e.printStackTrace(); + CodePushUtils.log(e); promise.reject(e); } return null; @@ -492,7 +492,7 @@ public void onHostDestroy() { promise.resolve(""); } catch(CodePushUnknownException e) { - e.printStackTrace(); + CodePushUtils.log(e); promise.reject(e); } @@ -508,7 +508,7 @@ public void isFailedUpdate(String packageHash, Promise promise) { try { promise.resolve(mSettingsManager.isFailedHash(packageHash)); } catch(CodePushUnknownException e) { - e.printStackTrace(); + CodePushUtils.log(e); promise.reject(e); } } @@ -522,7 +522,7 @@ public void isFirstRun(String packageHash, Promise promise) { && packageHash.equals(mUpdateManager.getCurrentPackageHash()); promise.resolve(isFirstRun); } catch(CodePushUnknownException e) { - e.printStackTrace(); + CodePushUtils.log(e); promise.reject(e); } } @@ -533,7 +533,7 @@ public void notifyApplicationReady(Promise promise) { mSettingsManager.removePendingUpdate(); promise.resolve(""); } catch(CodePushUnknownException e) { - e.printStackTrace(); + CodePushUtils.log(e); promise.reject(e); } } @@ -543,7 +543,7 @@ public void recordStatusReported(ReadableMap statusReport) { try { mTelemetryManager.recordStatusReported(statusReport); } catch(CodePushUnknownException e) { - e.printStackTrace(); + CodePushUtils.log(e); } } @@ -560,7 +560,7 @@ public void restartApp(boolean onlyIfUpdateIsPending, Promise promise) { promise.resolve(false); } catch(CodePushUnknownException e) { - e.printStackTrace(); + CodePushUtils.log(e); promise.reject(e); } } @@ -570,7 +570,7 @@ public void saveStatusReportForRetry(ReadableMap statusReport) { try { mTelemetryManager.saveStatusReportForRetry(statusReport); } catch(CodePushUnknownException e) { - e.printStackTrace(); + CodePushUtils.log(e); } } @@ -587,9 +587,9 @@ public void downloadAndReplaceCurrentBundle(String remoteBundleUrl) { } } } catch(CodePushUnknownException e) { - e.printStackTrace(); + CodePushUtils.log(e); } catch(CodePushMalformedDataException e) { - e.printStackTrace(); + CodePushUtils.log(e); } } } From 6850dd59ad7553e7da9f58ba0fb20c9eaefe6e13 Mon Sep 17 00:00:00 2001 From: Alexander Goncharov Date: Thu, 24 May 2018 14:44:07 +0300 Subject: [PATCH 4/4] Combined catches with the same handler. --- .../microsoft/codepush/react/CodePushNativeModule.java | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java index 8ba839b1b..4ad7701e1 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java @@ -255,14 +255,11 @@ public void dispatchDownloadProgressEvent() { JSONObject newPackage = mUpdateManager.getPackage(CodePushUtils.tryGetString(updatePackage, CodePushConstants.PACKAGE_HASH_KEY)); promise.resolve(CodePushUtils.convertJsonObjectToWritable(newPackage)); - } catch (IOException e) { - CodePushUtils.log(e); - promise.reject(e); } catch (CodePushInvalidUpdateException e) { CodePushUtils.log(e); mSettingsManager.saveFailedUpdate(CodePushUtils.convertReadableToJsonObject(updatePackage)); promise.reject(e); - } catch (CodePushUnknownException e) { + } catch (IOException | CodePushUnknownException e) { CodePushUtils.log(e); promise.reject(e); } @@ -586,9 +583,7 @@ public void downloadAndReplaceCurrentBundle(String remoteBundleUrl) { throw new CodePushUnknownException("Unable to replace current bundle", e); } } - } catch(CodePushUnknownException e) { - CodePushUtils.log(e); - } catch(CodePushMalformedDataException e) { + } catch(CodePushUnknownException | CodePushMalformedDataException e) { CodePushUtils.log(e); } }