Skip to content

Commit a1cdce8

Browse files
committed
feat(rn-native-module): handle errors from shared storage
1 parent 889be07 commit a1cdce8

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

packages/async-storage/android/src/main/kotlin/org/asyncstorage/storage/PersistentStorage.kt

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package org.asyncstorage.storage
22

33
import android.content.Context
4+
import com.facebook.react.bridge.Arguments
45
import com.facebook.react.bridge.Promise
56
import com.facebook.react.bridge.ReadableArray
7+
import com.facebook.react.bridge.WritableMap
68
import kotlin.coroutines.CoroutineContext
79
import kotlin.coroutines.EmptyCoroutineContext
810
import kotlinx.coroutines.CancellationException
@@ -12,6 +14,7 @@ import kotlinx.coroutines.SupervisorJob
1214
import kotlinx.coroutines.launch
1315
import kotlinx.coroutines.plus
1416
import org.asyncstorage.shared_storage.SharedStorage
17+
import org.asyncstorage.shared_storage.StorageException
1518

1619
private val createStorageScope = { name: String ->
1720
CoroutineScope(SupervisorJob() + CoroutineName(name))
@@ -69,7 +72,26 @@ private fun <T> CoroutineScope.lunchWithRejection(promise: Promise, block: suspe
6972
} catch (e: CancellationException) {
7073
throw e
7174
} catch (e: Exception) {
72-
promise.reject(code = "AsyncStorageException", message = e.message, throwable = e)
75+
76+
var userInfo: WritableMap? = null
77+
78+
if (e is StorageException) {
79+
userInfo =
80+
Arguments.createMap().also {
81+
if (e is StorageException.SqliteException) {
82+
it.putString("type", "SqliteException")
83+
} else {
84+
it.putString("type", "OtherException")
85+
}
86+
}
87+
}
88+
89+
promise.reject(
90+
code = "AsyncStorageError",
91+
message = e.message,
92+
throwable = e,
93+
userInfo = userInfo,
94+
)
7395
}
7496
}
7597
}

packages/async-storage/apple/storage/PersistentStorage.swift

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,33 @@ func runWithReject(_ reject: @escaping RCTPromiseRejectBlock, block: @escaping (
9191
Task {
9292
do {
9393
try await block()
94-
} catch {
95-
if error is CancellationError {
96-
throw error
94+
} catch let error as CancellationError {
95+
throw error
96+
} catch let error as NSError {
97+
if let exception = error.getStorageException() {
98+
reject("AsyncStorageError", exception.localizedDescription, exception)
99+
} else {
100+
reject("AsyncStorageError", error.localizedDescription, error)
97101
}
102+
} catch {
98103
reject("AsyncStorageError", error.localizedDescription, error)
99104
}
100105
}
101106
}
107+
108+
extension NSError {
109+
func getStorageException() -> NSError? {
110+
guard let exception = userInfo["KotlinException"] as? StorageException else {
111+
return nil
112+
}
113+
114+
if exception is StorageException.SqliteException {
115+
return NSError(domain: "SqliteException", code: 0, userInfo: [NSLocalizedDescriptionKey: exception.message ?? exception.description(), "type": "SqliteException"])
116+
117+
} else if exception is StorageException.SqliteException {
118+
return NSError(domain: "OtherException", code: 0, userInfo: [NSLocalizedDescriptionKey: exception.message ?? exception.description(), "type": "OtherException"])
119+
}
120+
121+
return nil
122+
}
123+
}

0 commit comments

Comments
 (0)