Skip to content

Commit 2de1a47

Browse files
[Internal] Improve Lock Error handling (#69)
1 parent f28111a commit 2de1a47

File tree

4 files changed

+66
-89
lines changed

4 files changed

+66
-89
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
## Unreleased
44

5+
* Fix null values in CRUD entries being reported as strings.
56
* [Internal] Instantiate Kotlin Kermit logger directly.
7+
* [Internal] Improved connection context error handling.
68

79
## 1.4.0
810

Package.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

44
import PackageDescription
5+
56
let packageName = "PowerSync"
67

78
// Set this to the absolute path of your Kotlin SDK checkout if you want to use a local Kotlin
@@ -31,8 +32,8 @@ if let kotlinSdkPath = localKotlinSdkOverride {
3132
// Not using a local build, so download from releases
3233
conditionalTargets.append(.binaryTarget(
3334
name: "PowerSyncKotlin",
34-
url: "https://github.com/powersync-ja/powersync-kotlin/releases/download/v1.4.0/PowersyncKotlinRelease.zip",
35-
checksum: "e800db216fc1c9722e66873deb4f925530267db6dbd5e2114dd845cc62c28cd9"
35+
url: "https://github.com/powersync-ja/powersync-kotlin/releases/download/v1.5.0/PowersyncKotlinRelease.zip",
36+
checksum: "cb1d717d28411aff0bfdeeaa837ae01514ebf5d64203dc565a9520a2912bae9d"
3637
))
3738
}
3839

@@ -59,7 +60,8 @@ let package = Package(
5960
// Products define the executables and libraries a package produces, making them visible to other packages.
6061
.library(
6162
name: packageName,
62-
targets: ["PowerSync"]),
63+
targets: ["PowerSync"]
64+
)
6365
],
6466
dependencies: conditionalDependencies,
6567
targets: [
@@ -70,10 +72,11 @@ let package = Package(
7072
dependencies: [
7173
kotlinTargetDependency,
7274
.product(name: "PowerSyncSQLiteCore", package: corePackageName)
73-
]),
75+
]
76+
),
7477
.testTarget(
7578
name: "PowerSyncTests",
7679
dependencies: ["PowerSync"]
77-
),
80+
)
7881
] + conditionalTargets
7982
)

Sources/PowerSync/Kotlin/KotlinPowerSyncDatabaseImpl.swift

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,7 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
275275
return try await wrapPowerSyncException {
276276
try safeCast(
277277
await kotlinDatabase.writeLock(
278-
callback: LockCallback(
279-
callback: callback
280-
)
278+
callback: wrapLockContext(callback: callback)
281279
),
282280
to: R.self
283281
)
@@ -290,9 +288,7 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
290288
return try await wrapPowerSyncException {
291289
try safeCast(
292290
await kotlinDatabase.writeTransaction(
293-
callback: TransactionCallback(
294-
callback: callback
295-
)
291+
callback: wrapTransactionContext(callback: callback)
296292
),
297293
to: R.self
298294
)
@@ -307,9 +303,7 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
307303
return try await wrapPowerSyncException {
308304
try safeCast(
309305
await kotlinDatabase.readLock(
310-
callback: LockCallback(
311-
callback: callback
312-
)
306+
callback: wrapLockContext(callback: callback)
313307
),
314308
to: R.self
315309
)
@@ -322,9 +316,7 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
322316
return try await wrapPowerSyncException {
323317
try safeCast(
324318
await kotlinDatabase.readTransaction(
325-
callback: TransactionCallback(
326-
callback: callback
327-
)
319+
callback: wrapTransactionContext(callback: callback)
328320
),
329321
to: R.self
330322
)
@@ -372,11 +364,11 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
372364
}
373365
)
374366

375-
let rootPages = rows.compactMap { r in
376-
if (r.opcode == "OpenRead" || r.opcode == "OpenWrite") &&
377-
r.p3 == 0 && r.p2 != 0
367+
let rootPages = rows.compactMap { row in
368+
if (row.opcode == "OpenRead" || row.opcode == "OpenWrite") &&
369+
row.p3 == 0 && row.p2 != 0
378370
{
379-
return r.p2
371+
return row.p2
380372
}
381373
return nil
382374
}
@@ -389,7 +381,7 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
389381
message: "Failed to convert pages data to UTF-8 string"
390382
)
391383
}
392-
384+
393385
let tableRows = try await getAll(
394386
sql: "SELECT tbl_name FROM sqlite_master WHERE rootpage IN (SELECT json_each.value FROM json_each(?))",
395387
parameters: [
@@ -414,3 +406,50 @@ private struct ExplainQueryResult {
414406
let p2: Int64
415407
let p3: Int64
416408
}
409+
410+
extension Error {
411+
func toPowerSyncError() -> PowerSyncKotlin.PowerSyncException {
412+
return PowerSyncKotlin.PowerSyncException(
413+
message: localizedDescription,
414+
cause: PowerSyncKotlin.KotlinThrowable(message: localizedDescription)
415+
)
416+
}
417+
}
418+
419+
func wrapLockContext(
420+
callback: @escaping (any ConnectionContext) throws -> Any
421+
) throws -> PowerSyncKotlin.ThrowableLockCallback {
422+
PowerSyncKotlin.wrapContextHandler { kotlinContext in
423+
do {
424+
return try PowerSyncKotlin.PowerSyncResult.Success(
425+
value: callback(
426+
KotlinConnectionContext(
427+
ctx: kotlinContext
428+
)
429+
))
430+
} catch {
431+
return PowerSyncKotlin.PowerSyncResult.Failure(
432+
exception: error.toPowerSyncError()
433+
)
434+
}
435+
}
436+
}
437+
438+
func wrapTransactionContext(
439+
callback: @escaping (any Transaction) throws -> Any
440+
) throws -> PowerSyncKotlin.ThrowableTransactionCallback {
441+
PowerSyncKotlin.wrapTransactionContextHandler { kotlinContext in
442+
do {
443+
return try PowerSyncKotlin.PowerSyncResult.Success(
444+
value: callback(
445+
KotlinTransactionContext(
446+
ctx: kotlinContext
447+
)
448+
))
449+
} catch {
450+
return PowerSyncKotlin.PowerSyncResult.Failure(
451+
exception: error.toPowerSyncError()
452+
)
453+
}
454+
}
455+
}

Sources/PowerSync/Kotlin/TransactionCallback.swift

Lines changed: 0 additions & 67 deletions
This file was deleted.

0 commit comments

Comments
 (0)