diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePushUpdateManager.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePushUpdateManager.java index f5e859a0..0bd72a72 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/CodePushUpdateManager.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/CodePushUpdateManager.java @@ -178,6 +178,13 @@ public void downloadPackage(JSONObject updatePackage, String expectedBundleFileN } connection.setRequestProperty("Accept-Encoding", "identity"); + + int responseCode = connection.getResponseCode(); + if (responseCode < 200 || responseCode >= 300) { + throw new CodePushUnknownException("Error downloading update package. Response code: " + + responseCode + ". Response body: " + NetworkUtils.readStreamToString(connection.getErrorStream())); + } + bin = new BufferedInputStream(connection.getInputStream()); long totalBytes = connection.getContentLength(); @@ -349,6 +356,13 @@ public void downloadAndReplaceCurrentBundle(String remoteBundleUrl, String bundl try { downloadUrl = new URL(remoteBundleUrl); connection = (HttpURLConnection) (downloadUrl.openConnection()); + + int responseCode = connection.getResponseCode(); + if (responseCode < 200 || responseCode >= 300) { + throw new CodePushUnknownException("Error downloading update package. Response code: " + + responseCode + ". Response body: " + NetworkUtils.readStreamToString(connection.getErrorStream())); + } + bin = new BufferedInputStream(connection.getInputStream()); File downloadFile = new File(getCurrentPackageBundlePath(bundleFileName)); downloadFile.delete(); diff --git a/android/app/src/main/java/com/microsoft/codepush/react/NetworkUtils.kt b/android/app/src/main/java/com/microsoft/codepush/react/NetworkUtils.kt new file mode 100644 index 00000000..2c360b39 --- /dev/null +++ b/android/app/src/main/java/com/microsoft/codepush/react/NetworkUtils.kt @@ -0,0 +1,9 @@ +@file:JvmName("NetworkUtils") + +package com.microsoft.codepush.react + +import java.io.InputStream + +fun readStreamToString(inputStream: InputStream?): String { + return inputStream?.bufferedReader()?.use { it.readText() } ?: "" +}