fix(patch): prevent NullPointerException crash in react-native-zip-a…#334
Conversation
…rchive Null ex.getMessage() calls in RNZipArchiveModule were crashing the app instead of rejecting the promise. Added null-safe fallbacks on all four affected reject sites and a safeReject helper as a last-resort guard. Also fixes a double switch on an uncast double in getCompressionLevel.
There was a problem hiding this comment.
Your free trial has ended. If you'd like to continue receiving code reviews, you can add a payment method here.
There was a problem hiding this comment.
Code Review
This pull request introduces a safeReject helper method in RNZipArchiveModule.java to handle promise rejections safely and prevent bridge-related crashes. It also updates several catch blocks to ensure a default error message is provided when an exception's message is null. The review feedback suggests consistently using the new safeReject helper in all modified catch blocks and replacing hardcoded error strings with constants to improve maintainability and consistency.
|
|
||
| } catch (Exception ex) { | ||
| - promise.reject("RNZipArchiveError", ex.getMessage()); | ||
| + promise.reject("RNZipArchiveError", ex.getMessage() != null ? ex.getMessage() : "unknown error"); |
There was a problem hiding this comment.
The safeReject helper should be used here to ensure consistent protection against bridge-related crashes, aligning with the requirement to refactor common operations into a single call across exit paths. Additionally, the hardcoded string 'RNZipArchiveError' should be replaced with a const val defined in a companion object to improve maintainability.
safeReject(promise, ERROR_CODE_ZIP_ARCHIVE, ex.message ?: "unknown error", ex);
References
- Avoid code duplication by refactoring common operations into a single call, especially when they appear in multiple exit paths of a function.
- Define string literals as
const valin acompanion objectand reuse them throughout the module to avoid hardcoding and improve maintainability.
|
|
||
| } catch (Exception ex) { | ||
| - promise.reject("RNZipArchiveError", ex.getMessage()); | ||
| + promise.reject("RNZipArchiveError", ex.getMessage() != null ? ex.getMessage() : "unknown error"); |
There was a problem hiding this comment.
This rejection site should also use the safeReject helper to ensure consistent protection against bridge-related crashes. Per the repository rules, the error code string should be defined as a const val in a companion object.
safeReject(promise, ERROR_CODE_ZIP_ARCHIVE, ex.message ?: "unknown error", ex);
References
- Avoid code duplication by refactoring common operations into a single call, especially when they appear in multiple exit paths of a function.
- Define string literals as
const valin acompanion objectand reuse them throughout the module to avoid hardcoding and improve maintainability.
| promise.resolve(destFile); | ||
| } catch (Exception ex) { | ||
| - promise.reject("RNZipArchiveError", ex.getMessage()); | ||
| + promise.reject("RNZipArchiveError", ex.getMessage() != null ? ex.getMessage() : "unknown error"); |
There was a problem hiding this comment.
This rejection site should also use the safeReject helper for consistency and crash prevention. In addition, avoid hardcoding the error string by defining it as a const val in a companion object.
safeReject(promise, ERROR_CODE_ZIP_ARCHIVE, ex.message ?: "unknown error", ex);
References
- Avoid code duplication by refactoring common operations into a single call, especially when they appear in multiple exit paths of a function.
- Define string literals as
const valin acompanion objectand reuse them throughout the module to avoid hardcoding and improve maintainability.
…archive All promise.reject sites now use safeReject helper to guard against null exception messages crashing the React Native bridge on Android.
|



Summary
ex.getMessage()andex.getLocalizedMessage()can returnnullin Java for certain exception types, causingPromiseImpl.rejectto crash with aNullPointerExceptionon the React Native bridgesafeReject()helper that null-checks bothcodeandmessagebefore callingpromise.reject, with a try/catch as a last-resort guardsafeRejectconsistently across all 4 reject sites inRNZipArchiveModule.javaERROR_CODEconstant to avoid hardcoded stringsswitch (compressionLevel)toswitch ((int) compressionLevel)to correct an uncast doubleTest plan