Skip to content

Commit 0bc83db

Browse files
author
Krzysztof Borowy
committed
more error handling
1 parent 5d2e64f commit 0bc83db

File tree

4 files changed

+18
-21
lines changed

4 files changed

+18
-21
lines changed

android/src/main/java/com/reactnativecommunity/asyncstorage/next/ArgumentHelpers.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ import com.facebook.react.bridge.ReadableArray
66
fun ReadableArray.toEntryList(): List<Entry> {
77
val list = mutableListOf<Entry>()
88
for (keyValue in this.toArrayList()) {
9-
if (keyValue !is ArrayList<*>) {
9+
if (keyValue !is ArrayList<*> || keyValue.size != 2) {
1010
throw AsyncStorageError.invalidKeyValueFormat()
1111
}
1212
val key = keyValue[0]
1313
val value = keyValue[1]
1414

1515
if (key == null || key !is String) {
16-
throw AsyncStorageError.keyIsNull()
16+
when (key) {
17+
null -> throw AsyncStorageError.keyIsNull()
18+
!is String -> throw AsyncStorageError.keyNotString()
19+
}
1720
}
1821

1922
if (value !is String) {
@@ -26,7 +29,7 @@ fun ReadableArray.toEntryList(): List<Entry> {
2629
}
2730

2831
fun ReadableArray.toKeyList(): List<String> {
29-
val list = this.toArrayList().toList()
32+
val list = this.toArrayList()
3033

3134
for (item in list) {
3235
if (item !is String) {

android/src/main/java/com/reactnativecommunity/asyncstorage/next/ErrorHelpers.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@ internal class AsyncStorageError private constructor(val errorMessage: String) :
2323
Throwable(errorMessage) {
2424

2525
companion object {
26-
fun keyIsNull() = AsyncStorageError("Key cannot be null")
26+
fun keyIsNull() = AsyncStorageError("Key cannot be null.")
2727

28-
fun invalidKeyValueFormat() =
29-
AsyncStorageError("Invalid key-value format. Expected a list of [key, value] list.")
28+
fun keyNotString() = AsyncStorageError("Provided key is not string. Only strings are supported as storage key.")
3029

3130
fun valueNotString(key: String?): AsyncStorageError {
3231
val detail = if (key == null) "Provided value" else "Value for key \"$key\""
33-
return AsyncStorageError("$detail is not of type String")
32+
return AsyncStorageError("$detail is not a string. Only strings are supported as a value.")
3433
}
34+
35+
fun invalidKeyValueFormat() =
36+
AsyncStorageError("Invalid key-value format. Expected a list of [key, value] list.")
37+
3538
}
3639
}

android/src/main/java/com/reactnativecommunity/asyncstorage/next/StorageModule.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class StorageModule(reactContext: ReactContext) : ReactContextBaseJavaModule(),
2323
/**
2424
* Todo:
2525
* - MultiMerge
26+
* - Documenting the migration, access from Brownfield and error handling
2627
*/
2728

2829
@ReactMethod

example/examples/Basic.js

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,11 @@ function NextExample() {
4444
}
4545

4646
async function crashKeyNull() {
47-
await AsyncStorage.setItem(null, 435345);
47+
await AsyncStorage.setItem(null, '435345');
4848
}
4949

50-
function unknownCrash() {
51-
// very unlikely to happen, unless module used directly
52-
return new Promise((res, rej) => {
53-
RCTAsyncStorage.multiSet(['key', 'crash'], function(errors) {
54-
if (errors) {
55-
const message = Array.isArray(errors) ? errors[0].message : errors.message;
56-
rej(new Error(message));
57-
} else {
58-
res();
59-
}
60-
});
61-
});
50+
async function crashKeyNotString() {
51+
await AsyncStorage.setItem(432, '435345');
6252
}
6353

6454
async function removeValue() {
@@ -88,8 +78,8 @@ function NextExample() {
8878
<Text style={{fontSize: 16, fontWeight: '700'}}>Crash scenarios</Text>
8979
<View style={{flexDirection: 'row', justifyContent: 'space-around'}}>
9080
<Button title="Key null" onPress={runWithCatch(crashKeyNull)}/>
81+
<Button title="Key not string" onPress={runWithCatch(crashKeyNotString)}/>
9182
<Button title="Wrong value type" onPress={runWithCatch(crashValueType)}/>
92-
<Button title="Other crash" onPress={runWithCatch(unknownCrash)}/>
9383
</View>
9484
</View>
9585

0 commit comments

Comments
 (0)