Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 9cb4e54

Browse files
committed
revert runtime exception changes
1 parent 7916856 commit 9cb4e54

File tree

8 files changed

+64
-45
lines changed

8 files changed

+64
-45
lines changed

android/app/src/main/java/com/microsoft/codepush/react/CodePush.java

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public CodePush(String deploymentKey, Activity mainActivity, boolean isDebugMode
9999
appVersion = pInfo.versionName;
100100
buildVersion = pInfo.versionCode;
101101
} catch (PackageManager.NameNotFoundException e) {
102-
CodePushUtils.logException("Unable to get package info for " + applicationContext.getPackageName(), e);
102+
throw new CodePushUnknownException("Unable to get package info for " + applicationContext.getPackageName(), e);
103103
}
104104

105105
if (currentInstance != null) {
@@ -130,14 +130,13 @@ private long getBinaryResourcesModifiedTime() {
130130
ZipEntry classesDexEntry = applicationFile.getEntry(RESOURCES_BUNDLE);
131131
return classesDexEntry.getTime();
132132
} catch (PackageManager.NameNotFoundException | IOException e) {
133-
CodePushUtils.logException("Error in getting file information about compiled resources", e);
134-
return -1;
133+
throw new CodePushUnknownException("Error in getting file information about compiled resources", e);
135134
} finally {
136135
if (applicationFile != null) {
137136
try {
138137
applicationFile.close();
139138
} catch (IOException e) {
140-
CodePushUtils.logException("Error in closing application file.", e);
139+
throw new CodePushUnknownException("Error in closing application file.", e);
141140
}
142141
}
143142
}
@@ -195,8 +194,7 @@ public String getBundleUrlInternal(String assetsBundleFileName) {
195194
return binaryJsBundleUrl;
196195
}
197196
} catch (NumberFormatException e) {
198-
CodePushUtils.logException("Error in closing application file.", e);
199-
return binaryJsBundleUrl;
197+
throw new CodePushUnknownException("Error in reading binary modified date from package metadata", e);
200198
}
201199
}
202200

@@ -254,7 +252,7 @@ private void initializeUpdateAfterRestart() {
254252
}
255253
} catch (JSONException e) {
256254
// Should not happen.
257-
CodePushUtils.logException("Unable to read pending update metadata stored in SharedPreferences", e);
255+
throw new CodePushUnknownException("Unable to read pending update metadata stored in SharedPreferences", e);
258256
}
259257
}
260258
}
@@ -270,7 +268,7 @@ private boolean isFailedHash(String packageHash) {
270268
return true;
271269
}
272270
} catch (JSONException e) {
273-
CodePushUtils.logException("Unable to read failedUpdates data stored in SharedPreferences.", e);
271+
throw new CodePushUnknownException("Unable to read failedUpdates data stored in SharedPreferences.", e);
274272
}
275273
}
276274
}
@@ -287,8 +285,7 @@ private boolean isPendingUpdate(String packageHash) {
287285
(packageHash == null || pendingUpdate.getString(PENDING_UPDATE_HASH_KEY).equals(packageHash));
288286
}
289287
catch (JSONException e) {
290-
CodePushUtils.logException("Unable to read pending update metadata in isPendingUpdate.", e);
291-
return false;
288+
throw new CodePushUnknownException("Unable to read pending update metadata in isPendingUpdate.", e);
292289
}
293290
}
294291

@@ -320,9 +317,8 @@ private void saveFailedUpdate(ReadableMap failedPackage) {
320317
failedUpdates = new JSONArray(failedUpdatesString);
321318
} catch (JSONException e) {
322319
// Should not happen.
323-
CodePushUtils.logException("Unable to parse failed updates information " +
320+
throw new CodePushMalformedDataException("Unable to parse failed updates information " +
324321
failedUpdatesString + " stored in SharedPreferences", e);
325-
failedUpdates = new JSONArray();
326322
}
327323
}
328324

@@ -340,7 +336,7 @@ private void savePendingUpdate(String packageHash, boolean isLoading) {
340336
settings.edit().putString(PENDING_UPDATE_KEY, pendingUpdate.toString()).commit();
341337
} catch (JSONException e) {
342338
// Should not happen.
343-
CodePushUtils.logException("Unable to save pending update.", e);
339+
throw new CodePushUnknownException("Unable to save pending update.", e);
344340
}
345341
}
346342

@@ -559,7 +555,7 @@ protected Void doInBackground(Void... params) {
559555
return null;
560556
}
561557
} catch (JSONException e) {
562-
CodePushUtils.logException("Unable to read failed updates information stored in SharedPreferences.", e);
558+
throw new CodePushUnknownException("Unable to read failed updates information stored in SharedPreferences.", e);
563559
}
564560
}
565561
} else if (didUpdate) {
@@ -596,8 +592,7 @@ protected Void doInBackground(Void... params) {
596592

597593
String pendingHash = CodePushUtils.tryGetString(updatePackage, PACKAGE_HASH_KEY);
598594
if (pendingHash == null) {
599-
CodePushUtils.log("Update package to be installed has no hash.");
600-
return null;
595+
throw new CodePushUnknownException("Update package to be installed has no hash.");
601596
} else {
602597
savePendingUpdate(pendingHash, /* isLoading */false);
603598
}
@@ -689,7 +684,7 @@ public void downloadAndReplaceCurrentBundle(String remoteBundleUrl) {
689684
try {
690685
codePushPackage.downloadAndReplaceCurrentBundle(remoteBundleUrl, assetsBundleFileName);
691686
} catch (IOException e) {
692-
CodePushUtils.logException("Unable to replace current bundle", e);
687+
throw new CodePushUnknownException("Unable to replace current bundle", e);
693688
}
694689
}
695690
}

android/app/src/main/java/com/microsoft/codepush/react/CodePushDialog.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public void onClick(DialogInterface dialog, int which) {
3636
case DialogInterface.BUTTON_NEGATIVE:
3737
successCallback.invoke(1);
3838
break;
39+
default:
40+
throw new CodePushUnknownException("Unknown button ID pressed.");
3941
}
4042
}
4143
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.microsoft.codepush.react;
2+
3+
import java.net.MalformedURLException;
4+
5+
public class CodePushMalformedDataException extends RuntimeException {
6+
public CodePushMalformedDataException(String path, Throwable cause) {
7+
super("Unable to parse contents of " + path + ", the file may be corrupted.", cause);
8+
}
9+
public CodePushMalformedDataException(String url, MalformedURLException cause) {
10+
super("The package has an invalid downloadUrl: " + url, cause);
11+
}
12+
}

android/app/src/main/java/com/microsoft/codepush/react/CodePushPackage.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ public WritableMap getCurrentPackageInfo() {
7272
return CodePushUtils.getWritableMapFromFile(statusFilePath);
7373
} catch (IOException e) {
7474
// Should not happen.
75-
CodePushUtils.logException("Error getting current package info" , e);
76-
return new WritableNativeMap();
75+
throw new CodePushUnknownException("Error getting current package info" , e);
7776
}
7877
}
7978

@@ -82,7 +81,7 @@ public void updateCurrentPackageInfo(ReadableMap packageInfo) {
8281
CodePushUtils.writeReadableMapToFile(packageInfo, getStatusFilePath());
8382
} catch (IOException e) {
8483
// Should not happen.
85-
CodePushUtils.logException("Error updating current package info" , e);
84+
throw new CodePushUnknownException("Error updating current package info" , e);
8685
}
8786
}
8887

@@ -212,18 +211,20 @@ public void downloadPackage(ReadableMap updatePackage, String expectedBundleFile
212211
}
213212

214213
if (totalBytes != receivedBytes) {
215-
CodePushUtils.log("Received " + receivedBytes + " bytes, expected " + totalBytes);
214+
throw new CodePushUnknownException("Received " + receivedBytes + " bytes, expected " + totalBytes);
216215
}
217216

218217
isZip = ByteBuffer.wrap(header).getInt() == 0x504b0304;
218+
} catch (MalformedURLException e) {
219+
throw new CodePushMalformedDataException(downloadUrlString, e);
219220
} finally {
220221
try {
221222
if (bout != null) bout.close();
222223
if (fos != null) fos.close();
223224
if (bin != null) bin.close();
224225
if (connection != null) connection.disconnect();
225226
} catch (IOException e) {
226-
CodePushUtils.logException("Error closing IO resources.", e);
227+
throw new CodePushUnknownException("Error closing IO resources.", e);
227228
}
228229
}
229230

@@ -267,8 +268,9 @@ public void downloadPackage(ReadableMap updatePackage, String expectedBundleFile
267268
try {
268269
updatePackageJSON.put(RELATIVE_BUNDLE_PATH_KEY, relativeBundlePath);
269270
} catch (JSONException e) {
270-
CodePushUtils.logException("Unable to set key " + RELATIVE_BUNDLE_PATH_KEY +
271-
" to value " + relativeBundlePath + " in update package.", e);
271+
throw new CodePushUnknownException("Unable to set key " +
272+
RELATIVE_BUNDLE_PATH_KEY + " to value " + relativeBundlePath +
273+
" in update package.", e);
272274
}
273275

274276
updatePackage = CodePushUtils.convertJsonObjectToWritable(updatePackageJSON);
@@ -331,14 +333,16 @@ public void downloadAndReplaceCurrentBundle(String remoteBundleUrl, String bundl
331333
while ((numBytesRead = bin.read(data, 0, DOWNLOAD_BUFFER_SIZE)) >= 0) {
332334
bout.write(data, 0, numBytesRead);
333335
}
336+
} catch (MalformedURLException e) {
337+
throw new CodePushMalformedDataException(remoteBundleUrl, e);
334338
} finally {
335339
try {
336340
if (bout != null) bout.close();
337341
if (fos != null) fos.close();
338342
if (bin != null) bin.close();
339343
if (connection != null) connection.disconnect();
340344
} catch (IOException e) {
341-
CodePushUtils.logException("Error closing IO resources.", e);
345+
throw new CodePushUnknownException("Error closing IO resources.", e);
342346
}
343347
}
344348
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.microsoft.codepush.react;
2+
3+
class CodePushUnknownException extends RuntimeException {
4+
5+
public CodePushUnknownException(String message, Throwable cause) {
6+
super(message, cause);
7+
}
8+
9+
public CodePushUnknownException(String message) {
10+
super(message);
11+
}
12+
}

android/app/src/main/java/com/microsoft/codepush/react/CodePushUpdateUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ private static void addContentsOfFolderToManifest(String folderPath, String path
3636
manifest.add(relativePath + ":" + computeHash(new FileInputStream(file)));
3737
} catch (FileNotFoundException e) {
3838
// Should not happen.
39-
CodePushUtils.logException("Unable to compute hash of update contents.", e);
39+
throw new CodePushUnknownException("Unable to compute hash of update contents.", e);
4040
}
4141
}
4242
}
@@ -52,7 +52,7 @@ private static String computeHash(InputStream dataStream) {
5252
while (digestInputStream.read(byteBuffer) != -1);
5353
} catch (NoSuchAlgorithmException | IOException e) {
5454
// Should not happen.
55-
CodePushUtils.logException("Unable to compute hash of update contents.", e);
55+
throw new CodePushUnknownException("Unable to compute hash of update contents.", e);
5656
} finally {
5757
try {
5858
if (digestInputStream != null) digestInputStream.close();

android/app/src/main/java/com/microsoft/codepush/react/CodePushUtils.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static WritableArray convertJsonArrayToWritable(JSONArray jsonArr) {
3838
obj = jsonArr.get(i);
3939
} catch (JSONException jsonException) {
4040
// Should not happen.
41-
CodePushUtils.logException(i + " should be within bounds of array " + jsonArr.toString(), jsonException);
41+
throw new CodePushUnknownException(i + " should be within bounds of array " + jsonArr.toString(), jsonException);
4242
}
4343

4444
if (obj instanceof JSONObject)
@@ -56,7 +56,7 @@ else if (obj instanceof Boolean)
5656
else if (obj == null)
5757
arr.pushNull();
5858
else
59-
CodePushUtils.log("Unrecognized object: " + obj);
59+
throw new CodePushUnknownException("Unrecognized object: " + obj);
6060
}
6161

6262
return arr;
@@ -72,7 +72,7 @@ public static WritableMap convertJsonObjectToWritable(JSONObject jsonObj) {
7272
obj = jsonObj.get(key);
7373
} catch (JSONException jsonException) {
7474
// Should not happen.
75-
CodePushUtils.logException("Key " + key + " should exist in " + jsonObj.toString() + ".", jsonException);
75+
throw new CodePushUnknownException("Key " + key + " should exist in " + jsonObj.toString() + ".", jsonException);
7676
}
7777

7878
if (obj instanceof JSONObject)
@@ -90,7 +90,7 @@ else if (obj instanceof Boolean)
9090
else if (obj == null)
9191
map.putNull(key);
9292
else
93-
CodePushUtils.log("Unrecognized object: " + obj);
93+
throw new CodePushUnknownException("Unrecognized object: " + obj);
9494
}
9595

9696
return map;
@@ -124,7 +124,7 @@ public static JSONArray convertReadableToJsonArray(ReadableArray arr) {
124124
try {
125125
jsonArr.put(number.doubleValue());
126126
} catch (JSONException jsonException) {
127-
CodePushUtils.log("Unable to put value " + arr.getDouble(i) + " in JSONArray");
127+
throw new CodePushUnknownException("Unable to put value " + arr.getDouble(i) + " in JSONArray");
128128
}
129129
}
130130
break;
@@ -167,10 +167,10 @@ public static JSONObject convertReadableToJsonObject(ReadableMap map) {
167167
jsonObj.put(key, null);
168168
break;
169169
default:
170-
CodePushUtils.log("Unrecognized type: " + type + " of key: " + key);
170+
throw new CodePushUnknownException("Unrecognized type: " + type + " of key: " + key);
171171
}
172172
} catch (JSONException jsonException) {
173-
CodePushUtils.logException("Error setting key: " + key + " in JSONObject", jsonException);
173+
throw new CodePushUnknownException("Error setting key: " + key + " in JSONObject", jsonException);
174174
}
175175
}
176176

@@ -203,8 +203,7 @@ public static WritableMap getWritableMapFromFile(String filePath) throws IOExcep
203203
return convertJsonObjectToWritable(json);
204204
} catch (JSONException jsonException) {
205205
// Should not happen
206-
CodePushUtils.logException(filePath, jsonException);
207-
return null;
206+
throw new CodePushMalformedDataException(filePath, jsonException);
208207
}
209208
}
210209

@@ -216,11 +215,6 @@ public static void logBundleUrl(String path) {
216215
log("Loading JS bundle from \"" + path + "\"");
217216
}
218217

219-
public static void logException(String message, Exception e) {
220-
log(message);
221-
e.printStackTrace();
222-
}
223-
224218
public static String tryGetString(ReadableMap map, String key) {
225219
try {
226220
return map.getString(key);

android/app/src/main/java/com/microsoft/codepush/react/FileUtils.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static void copyDirectoryContents(String sourceDirectoryPath, String dest
4747
if (fromBufferedStream != null) fromBufferedStream.close();
4848
if (destStream != null) destStream.close();
4949
} catch (IOException e) {
50-
CodePushUtils.logException("Error closing IO resources.", e);
50+
throw new CodePushUnknownException("Error closing IO resources.", e);
5151
}
5252
}
5353
}
@@ -110,8 +110,8 @@ public static void moveFile(File fileToMove, String newFolderPath, String newFil
110110

111111
File newFilePath = new File(newFolderPath, newFileName);
112112
if (!fileToMove.renameTo(newFilePath)) {
113-
CodePushUtils.log("Unable to move file from " + fileToMove.getAbsolutePath() +
114-
" to " + newFilePath.getAbsolutePath() + ".");
113+
throw new CodePushUnknownException("Unable to move file from " +
114+
fileToMove.getAbsolutePath() + " to " + newFilePath.getAbsolutePath() + ".");
115115
}
116116
}
117117

@@ -185,7 +185,7 @@ public static void unzipFile(File zipFile, String destination) throws IOExceptio
185185
if (bufferedStream != null) bufferedStream.close();
186186
if (fileStream != null) fileStream.close();
187187
} catch (IOException e) {
188-
CodePushUtils.logException("Error closing IO resources.", e);
188+
throw new CodePushUnknownException("Error closing IO resources.", e);
189189
}
190190
}
191191
}

0 commit comments

Comments
 (0)