Skip to content

Commit 73f2a79

Browse files
author
Krzysztof Borowy
committed
fix: throw error on invalid value
1 parent b04ffbf commit 73f2a79

File tree

3 files changed

+10
-12
lines changed

3 files changed

+10
-12
lines changed

android/src/main/java/com/reactnativecommunity/asyncstorage/AsyncStorageErrorUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ public class AsyncStorageErrorUtil {
3030
}
3131

3232
/* package */ static WritableMap getInvalidKeyError(@Nullable String key) {
33-
return getError(key, "Invalid key");
33+
return getError(key, "Invalid key - expected string.");
3434
}
3535

3636
/* package */ static WritableMap getInvalidValueError(@Nullable String key) {
37-
return getError(key, "Invalid Value");
37+
return getError(key, "Invalid Value - expected string.");
3838
}
3939

4040
/* package */ static WritableMap getDBError(@Nullable String key) {

android/src/main/java/com/reactnativecommunity/asyncstorage/AsyncStorageModule.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -218,23 +218,21 @@ protected void doInBackgroundGuarded(Void... params) {
218218
for (int idx=0; idx < keyValueArray.size(); idx++) {
219219
if (keyValueArray.getArray(idx).size() != 2) {
220220
error = AsyncStorageErrorUtil.getInvalidValueError(null);
221-
return;
222221
}
223222
if (keyValueArray.getArray(idx).getString(0) == null) {
224223
error = AsyncStorageErrorUtil.getInvalidKeyError(null);
225-
return;
226224
}
227225
if (keyValueArray.getArray(idx).getString(1) == null) {
228226
error = AsyncStorageErrorUtil.getInvalidValueError(null);
229-
return;
230227
}
231-
232-
statement.clearBindings();
233-
statement.bindString(1, keyValueArray.getArray(idx).getString(0));
234-
statement.bindString(2, keyValueArray.getArray(idx).getString(1));
235-
statement.execute();
228+
if(error == null) {
229+
statement.clearBindings();
230+
statement.bindString(1, keyValueArray.getArray(idx).getString(0));
231+
statement.bindString(2, keyValueArray.getArray(idx).getString(1));
232+
statement.execute();
233+
mReactDatabaseSupplier.get().setTransactionSuccessful();
234+
}
236235
}
237-
mReactDatabaseSupplier.get().setTransactionSuccessful();
238236
} catch (Exception e) {
239237
FLog.w(ReactConstants.TAG, e.getMessage(), e);
240238
error = AsyncStorageErrorUtil.getError(null, e.getMessage());

lib/AsyncStorage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ type MultiRequest = {|
5353
|};
5454

5555
function checkValueTypeNotString(value: any, usedKey: string) {
56-
if (typeof value !== 'string') {
56+
if (value && typeof value !== 'string') {
5757
console.warn(
5858
`[AsyncStorage] The value for key "${usedKey}" is not a string. This can lead to unexpected behavior/errors. Consider stringifying it.`,
5959
);

0 commit comments

Comments
 (0)