Skip to content

Commit 223c004

Browse files
propagate connector upload errors
1 parent 21dc8f4 commit 223c004

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

Sources/PowerSync/Kotlin/wrapQueryCursor.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,26 @@ func wrapQueryCursorTyped<RowType, ReturnType>(
6060
resultType
6161
)
6262
}
63+
64+
65+
/// Throws a `PowerSyncException` using a helper provided by the Kotlin SDK.
66+
/// We can't directly throw Kotlin `PowerSyncException`s from Swift, but we can delegate the throwing
67+
/// to the Kotlin implementation.
68+
/// Our Kotlin SDK methods handle thrown Kotlin `PowerSyncException` correctly.
69+
/// The flow of events is as follows
70+
/// Swift code calls `throwKotlinPowerSyncError`
71+
/// This method calls the Kotlin helper `throwPowerSyncException` which is annotated as being able to throw `PowerSyncException`
72+
/// The Kotlin helper throws the provided `PowerSyncException`. Since the method is annotated the exception propagates back to Swift, but in a form which can propagate back
73+
/// to any calling Kotlin stack.
74+
/// This only works for SKIEE methods which have an associated completion handler which handles annotated errors.
75+
/// This seems to only apply for Kotlin suspending function bindings.
76+
func throwKotlinPowerSyncError (message: String, cause: String? = nil) throws {
77+
try throwPowerSyncException(
78+
exception: PowerSyncKotlin.PowerSyncException(
79+
message: message,
80+
cause: PowerSyncKotlin.KotlinThrowable(
81+
message: cause ?? message
82+
)
83+
)
84+
)
85+
}

Sources/PowerSync/kotlin/PowerSyncBackendConnectorAdapter.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,31 @@ internal class PowerSyncBackendConnectorAdapter: KotlinPowerSyncBackendConnector
1212
self.swiftBackendConnector = swiftBackendConnector
1313
self.db = db
1414
}
15-
15+
1616
override func __fetchCredentials() async throws -> KotlinPowerSyncCredentials? {
1717
do {
1818
let result = try await swiftBackendConnector.fetchCredentials()
1919
return result?.kotlinCredentials
2020
} catch {
2121
db.logger.error("Error while fetching credentials", tag: logTag)
22+
/// We can't use throwKotlinPowerSyncError here since the Kotlin connector
23+
/// runs this in a Job - this seems to break the SKIEE error propagation.
24+
/// returning nil here should still cause a retry
2225
return nil
2326
}
2427
}
2528

2629
override func __uploadData(database: KotlinPowerSyncDatabase) async throws {
2730
do {
2831
// Pass the Swift DB protocal to the connector
29-
return try await swiftBackendConnector.uploadData(database: db)
32+
return try await swiftBackendConnector.uploadData(database: db)
3033
} catch {
3134
db.logger.error("Error while uploading data: \(error)", tag: logTag)
35+
// Relay the error to the Kotlin SDK
36+
try throwKotlinPowerSyncError(
37+
message: "Connector errored while uploading data: \(error.localizedDescription)",
38+
cause: error.localizedDescription,
39+
)
3240
}
3341
}
3442
}

0 commit comments

Comments
 (0)