From 9cb4e54a8114edec05a4ad4825518705f567f642 Mon Sep 17 00:00:00 2001 From: Geoffrey Goh Date: Mon, 25 Apr 2016 17:52:07 -0700 Subject: [PATCH] revert runtime exception changes --- .../microsoft/codepush/react/CodePush.java | 29 ++++++++----------- .../codepush/react/CodePushDialog.java | 2 ++ .../react/CodePushMalformedDataException.java | 12 ++++++++ .../codepush/react/CodePushPackage.java | 20 ++++++++----- .../react/CodePushUnknownException.java | 12 ++++++++ .../codepush/react/CodePushUpdateUtils.java | 4 +-- .../codepush/react/CodePushUtils.java | 22 +++++--------- .../microsoft/codepush/react/FileUtils.java | 8 ++--- 8 files changed, 64 insertions(+), 45 deletions(-) create mode 100644 android/app/src/main/java/com/microsoft/codepush/react/CodePushMalformedDataException.java create mode 100644 android/app/src/main/java/com/microsoft/codepush/react/CodePushUnknownException.java diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java index 551d0c4..c071928 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java @@ -99,7 +99,7 @@ public class CodePush implements ReactPackage { appVersion = pInfo.versionName; buildVersion = pInfo.versionCode; } catch (PackageManager.NameNotFoundException e) { - CodePushUtils.logException("Unable to get package info for " + applicationContext.getPackageName(), e); + throw new CodePushUnknownException("Unable to get package info for " + applicationContext.getPackageName(), e); } if (currentInstance != null) { @@ -130,14 +130,13 @@ public class CodePush implements ReactPackage { ZipEntry classesDexEntry = applicationFile.getEntry(RESOURCES_BUNDLE); return classesDexEntry.getTime(); } catch (PackageManager.NameNotFoundException | IOException e) { - CodePushUtils.logException("Error in getting file information about compiled resources", e); - return -1; + throw new CodePushUnknownException("Error in getting file information about compiled resources", e); } finally { if (applicationFile != null) { try { applicationFile.close(); } catch (IOException e) { - CodePushUtils.logException("Error in closing application file.", e); + throw new CodePushUnknownException("Error in closing application file.", e); } } } @@ -195,8 +194,7 @@ public class CodePush implements ReactPackage { return binaryJsBundleUrl; } } catch (NumberFormatException e) { - CodePushUtils.logException("Error in closing application file.", e); - return binaryJsBundleUrl; + throw new CodePushUnknownException("Error in reading binary modified date from package metadata", e); } } @@ -254,7 +252,7 @@ public class CodePush implements ReactPackage { } } catch (JSONException e) { // Should not happen. - CodePushUtils.logException("Unable to read pending update metadata stored in SharedPreferences", e); + throw new CodePushUnknownException("Unable to read pending update metadata stored in SharedPreferences", e); } } } @@ -270,7 +268,7 @@ public class CodePush implements ReactPackage { return true; } } catch (JSONException e) { - CodePushUtils.logException("Unable to read failedUpdates data stored in SharedPreferences.", e); + throw new CodePushUnknownException("Unable to read failedUpdates data stored in SharedPreferences.", e); } } } @@ -287,8 +285,7 @@ public class CodePush implements ReactPackage { (packageHash == null || pendingUpdate.getString(PENDING_UPDATE_HASH_KEY).equals(packageHash)); } catch (JSONException e) { - CodePushUtils.logException("Unable to read pending update metadata in isPendingUpdate.", e); - return false; + throw new CodePushUnknownException("Unable to read pending update metadata in isPendingUpdate.", e); } } @@ -320,9 +317,8 @@ public class CodePush implements ReactPackage { failedUpdates = new JSONArray(failedUpdatesString); } catch (JSONException e) { // Should not happen. - CodePushUtils.logException("Unable to parse failed updates information " + + throw new CodePushMalformedDataException("Unable to parse failed updates information " + failedUpdatesString + " stored in SharedPreferences", e); - failedUpdates = new JSONArray(); } } @@ -340,7 +336,7 @@ public class CodePush implements ReactPackage { settings.edit().putString(PENDING_UPDATE_KEY, pendingUpdate.toString()).commit(); } catch (JSONException e) { // Should not happen. - CodePushUtils.logException("Unable to save pending update.", e); + throw new CodePushUnknownException("Unable to save pending update.", e); } } @@ -559,7 +555,7 @@ public class CodePush implements ReactPackage { return null; } } catch (JSONException e) { - CodePushUtils.logException("Unable to read failed updates information stored in SharedPreferences.", e); + throw new CodePushUnknownException("Unable to read failed updates information stored in SharedPreferences.", e); } } } else if (didUpdate) { @@ -596,8 +592,7 @@ public class CodePush implements ReactPackage { String pendingHash = CodePushUtils.tryGetString(updatePackage, PACKAGE_HASH_KEY); if (pendingHash == null) { - CodePushUtils.log("Update package to be installed has no hash."); - return null; + throw new CodePushUnknownException("Update package to be installed has no hash."); } else { savePendingUpdate(pendingHash, /* isLoading */false); } @@ -689,7 +684,7 @@ public class CodePush implements ReactPackage { try { codePushPackage.downloadAndReplaceCurrentBundle(remoteBundleUrl, assetsBundleFileName); } catch (IOException e) { - CodePushUtils.logException("Unable to replace current bundle", e); + throw new CodePushUnknownException("Unable to replace current bundle", e); } } } diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePushDialog.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePushDialog.java index a4d8fa3..e8d19fb 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/CodePushDialog.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/CodePushDialog.java @@ -36,6 +36,8 @@ public class CodePushDialog extends ReactContextBaseJavaModule{ case DialogInterface.BUTTON_NEGATIVE: successCallback.invoke(1); break; + default: + throw new CodePushUnknownException("Unknown button ID pressed."); } } }; diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePushMalformedDataException.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePushMalformedDataException.java new file mode 100644 index 0000000..88b8200 --- /dev/null +++ b/android/app/src/main/java/com/microsoft/codepush/react/CodePushMalformedDataException.java @@ -0,0 +1,12 @@ +package com.microsoft.codepush.react; + +import java.net.MalformedURLException; + +public class CodePushMalformedDataException extends RuntimeException { + public CodePushMalformedDataException(String path, Throwable cause) { + super("Unable to parse contents of " + path + ", the file may be corrupted.", cause); + } + public CodePushMalformedDataException(String url, MalformedURLException cause) { + super("The package has an invalid downloadUrl: " + url, cause); + } +} \ No newline at end of file diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePushPackage.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePushPackage.java index 9b7d0c7..61cf1ea 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/CodePushPackage.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/CodePushPackage.java @@ -72,8 +72,7 @@ public class CodePushPackage { return CodePushUtils.getWritableMapFromFile(statusFilePath); } catch (IOException e) { // Should not happen. - CodePushUtils.logException("Error getting current package info" , e); - return new WritableNativeMap(); + throw new CodePushUnknownException("Error getting current package info" , e); } } @@ -82,7 +81,7 @@ public class CodePushPackage { CodePushUtils.writeReadableMapToFile(packageInfo, getStatusFilePath()); } catch (IOException e) { // Should not happen. - CodePushUtils.logException("Error updating current package info" , e); + throw new CodePushUnknownException("Error updating current package info" , e); } } @@ -212,10 +211,12 @@ public class CodePushPackage { } if (totalBytes != receivedBytes) { - CodePushUtils.log("Received " + receivedBytes + " bytes, expected " + totalBytes); + throw new CodePushUnknownException("Received " + receivedBytes + " bytes, expected " + totalBytes); } isZip = ByteBuffer.wrap(header).getInt() == 0x504b0304; + } catch (MalformedURLException e) { + throw new CodePushMalformedDataException(downloadUrlString, e); } finally { try { if (bout != null) bout.close(); @@ -223,7 +224,7 @@ public class CodePushPackage { if (bin != null) bin.close(); if (connection != null) connection.disconnect(); } catch (IOException e) { - CodePushUtils.logException("Error closing IO resources.", e); + throw new CodePushUnknownException("Error closing IO resources.", e); } } @@ -267,8 +268,9 @@ public class CodePushPackage { try { updatePackageJSON.put(RELATIVE_BUNDLE_PATH_KEY, relativeBundlePath); } catch (JSONException e) { - CodePushUtils.logException("Unable to set key " + RELATIVE_BUNDLE_PATH_KEY + - " to value " + relativeBundlePath + " in update package.", e); + throw new CodePushUnknownException("Unable to set key " + + RELATIVE_BUNDLE_PATH_KEY + " to value " + relativeBundlePath + + " in update package.", e); } updatePackage = CodePushUtils.convertJsonObjectToWritable(updatePackageJSON); @@ -331,6 +333,8 @@ public class CodePushPackage { while ((numBytesRead = bin.read(data, 0, DOWNLOAD_BUFFER_SIZE)) >= 0) { bout.write(data, 0, numBytesRead); } + } catch (MalformedURLException e) { + throw new CodePushMalformedDataException(remoteBundleUrl, e); } finally { try { if (bout != null) bout.close(); @@ -338,7 +342,7 @@ public class CodePushPackage { if (bin != null) bin.close(); if (connection != null) connection.disconnect(); } catch (IOException e) { - CodePushUtils.logException("Error closing IO resources.", e); + throw new CodePushUnknownException("Error closing IO resources.", e); } } } diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePushUnknownException.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePushUnknownException.java new file mode 100644 index 0000000..cb99f6c --- /dev/null +++ b/android/app/src/main/java/com/microsoft/codepush/react/CodePushUnknownException.java @@ -0,0 +1,12 @@ +package com.microsoft.codepush.react; + +class CodePushUnknownException extends RuntimeException { + + public CodePushUnknownException(String message, Throwable cause) { + super(message, cause); + } + + public CodePushUnknownException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePushUpdateUtils.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePushUpdateUtils.java index 4e9c84c..ee6d78d 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/CodePushUpdateUtils.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/CodePushUpdateUtils.java @@ -36,7 +36,7 @@ public class CodePushUpdateUtils { manifest.add(relativePath + ":" + computeHash(new FileInputStream(file))); } catch (FileNotFoundException e) { // Should not happen. - CodePushUtils.logException("Unable to compute hash of update contents.", e); + throw new CodePushUnknownException("Unable to compute hash of update contents.", e); } } } @@ -52,7 +52,7 @@ public class CodePushUpdateUtils { while (digestInputStream.read(byteBuffer) != -1); } catch (NoSuchAlgorithmException | IOException e) { // Should not happen. - CodePushUtils.logException("Unable to compute hash of update contents.", e); + throw new CodePushUnknownException("Unable to compute hash of update contents.", e); } finally { try { if (digestInputStream != null) digestInputStream.close(); diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePushUtils.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePushUtils.java index 780f1db..55752a6 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/CodePushUtils.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/CodePushUtils.java @@ -38,7 +38,7 @@ public class CodePushUtils { obj = jsonArr.get(i); } catch (JSONException jsonException) { // Should not happen. - CodePushUtils.logException(i + " should be within bounds of array " + jsonArr.toString(), jsonException); + throw new CodePushUnknownException(i + " should be within bounds of array " + jsonArr.toString(), jsonException); } if (obj instanceof JSONObject) @@ -56,7 +56,7 @@ public class CodePushUtils { else if (obj == null) arr.pushNull(); else - CodePushUtils.log("Unrecognized object: " + obj); + throw new CodePushUnknownException("Unrecognized object: " + obj); } return arr; @@ -72,7 +72,7 @@ public class CodePushUtils { obj = jsonObj.get(key); } catch (JSONException jsonException) { // Should not happen. - CodePushUtils.logException("Key " + key + " should exist in " + jsonObj.toString() + ".", jsonException); + throw new CodePushUnknownException("Key " + key + " should exist in " + jsonObj.toString() + ".", jsonException); } if (obj instanceof JSONObject) @@ -90,7 +90,7 @@ public class CodePushUtils { else if (obj == null) map.putNull(key); else - CodePushUtils.log("Unrecognized object: " + obj); + throw new CodePushUnknownException("Unrecognized object: " + obj); } return map; @@ -124,7 +124,7 @@ public class CodePushUtils { try { jsonArr.put(number.doubleValue()); } catch (JSONException jsonException) { - CodePushUtils.log("Unable to put value " + arr.getDouble(i) + " in JSONArray"); + throw new CodePushUnknownException("Unable to put value " + arr.getDouble(i) + " in JSONArray"); } } break; @@ -167,10 +167,10 @@ public class CodePushUtils { jsonObj.put(key, null); break; default: - CodePushUtils.log("Unrecognized type: " + type + " of key: " + key); + throw new CodePushUnknownException("Unrecognized type: " + type + " of key: " + key); } } catch (JSONException jsonException) { - CodePushUtils.logException("Error setting key: " + key + " in JSONObject", jsonException); + throw new CodePushUnknownException("Error setting key: " + key + " in JSONObject", jsonException); } } @@ -203,8 +203,7 @@ public class CodePushUtils { return convertJsonObjectToWritable(json); } catch (JSONException jsonException) { // Should not happen - CodePushUtils.logException(filePath, jsonException); - return null; + throw new CodePushMalformedDataException(filePath, jsonException); } } @@ -216,11 +215,6 @@ public class CodePushUtils { log("Loading JS bundle from \"" + path + "\""); } - public static void logException(String message, Exception e) { - log(message); - e.printStackTrace(); - } - public static String tryGetString(ReadableMap map, String key) { try { return map.getString(key); diff --git a/android/app/src/main/java/com/microsoft/codepush/react/FileUtils.java b/android/app/src/main/java/com/microsoft/codepush/react/FileUtils.java index fe9767b..fcc8cf5 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/FileUtils.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/FileUtils.java @@ -47,7 +47,7 @@ public class FileUtils { if (fromBufferedStream != null) fromBufferedStream.close(); if (destStream != null) destStream.close(); } catch (IOException e) { - CodePushUtils.logException("Error closing IO resources.", e); + throw new CodePushUnknownException("Error closing IO resources.", e); } } } @@ -110,8 +110,8 @@ public class FileUtils { File newFilePath = new File(newFolderPath, newFileName); if (!fileToMove.renameTo(newFilePath)) { - CodePushUtils.log("Unable to move file from " + fileToMove.getAbsolutePath() + - " to " + newFilePath.getAbsolutePath() + "."); + throw new CodePushUnknownException("Unable to move file from " + + fileToMove.getAbsolutePath() + " to " + newFilePath.getAbsolutePath() + "."); } } @@ -185,7 +185,7 @@ public class FileUtils { if (bufferedStream != null) bufferedStream.close(); if (fileStream != null) fileStream.close(); } catch (IOException e) { - CodePushUtils.logException("Error closing IO resources.", e); + throw new CodePushUnknownException("Error closing IO resources.", e); } } }