Skip to content

Commit 72dd7e1

Browse files
chore: improve error handling for kotlin errors (#20)
* chore: improve error handling for kotlin errors * chore: add red dot
1 parent 3afda29 commit 72dd7e1

File tree

8 files changed

+59
-32
lines changed

8 files changed

+59
-32
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
# Changelog
22

3+
## 1.0.0-Beta.5
4+
5+
* Implement improvements to errors originating in Kotlin so that they can be handled in Swift
6+
* Improve `__fetchCredentials`to log the error but not cause an app crash on error
7+
8+
39
## 1.0.0-Beta.4
10+
411
* Allow cursor to use column name to get value by including the following functions that accept a column name parameter:
512
`getBoolean`,`getBooleanOptional`,`getString`,`getStringOptional`, `getLong`,`getLongOptional`, `getDouble`,`getDoubleOptional`
613
* BREAKING CHANGE: This should not affect anyone but made `KotlinPowerSyncCredentials`, `KotlinPowerSyncDatabase` and `KotlinPowerSyncBackendConnector` private as these should never have been public.

Demo/PowerSyncExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
"kind" : "remoteSourceControl",
1616
"location" : "https://github.com/powersync-ja/powersync-kotlin.git",
1717
"state" : {
18-
"revision" : "7cd47ffc9dbec8fae4f9e067945ef2279015a90d",
19-
"version" : "1.0.0-BETA20.0"
18+
"revision" : "61d195816585f30260181dcbd157bf1660c9ac4e",
19+
"version" : "1.0.0-BETA22.0"
2020
}
2121
},
2222
{

Demo/PowerSyncExample/PowerSync/SupabaseConnector.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ class SupabaseConnector: PowerSyncBackendConnector {
7070

7171
override func fetchCredentials() async throws -> PowerSyncCredentials? {
7272
session = try await client.auth.session
73-
73+
7474
if (self.session == nil) {
7575
throw AuthError.sessionMissing
7676
}
77-
77+
7878
let token = session!.accessToken
79-
79+
8080
// userId is for debugging purposes only
8181
return PowerSyncCredentials(endpoint: self.powerSyncEndpoint, token: token, userId: currentUserID)
8282
}

Package.resolved

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"kind" : "remoteSourceControl",
66
"location" : "https://github.com/powersync-ja/powersync-kotlin.git",
77
"state" : {
8-
"revision" : "7cd47ffc9dbec8fae4f9e067945ef2279015a90d",
9-
"version" : "1.0.0-BETA20.0"
8+
"revision" : "61d195816585f30260181dcbd157bf1660c9ac4e",
9+
"version" : "1.0.0-BETA22.0"
1010
}
1111
},
1212
{

Package.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ let package = Package(
1616
targets: ["PowerSync"]),
1717
],
1818
dependencies: [
19-
.package(url: "https://github.com/powersync-ja/powersync-kotlin.git", exact: "1.0.0-BETA20.0"),
19+
.package(url: "https://github.com/powersync-ja/powersync-kotlin.git", exact: "1.0.0-BETA22.0"),
2020
.package(url: "https://github.com/powersync-ja/powersync-sqlite-core-swift.git", "0.3.9"..<"0.4.0")
2121
],
2222
targets: [

Sources/PowerSync/Kotlin/KotlinPowerSyncDatabaseImpl.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
153153
) -> AsyncStream<[RowType]> {
154154
AsyncStream { continuation in
155155
Task {
156-
for await values in self.kotlinDatabase.watch(
156+
for await values in try self.kotlinDatabase.watch(
157157
sql: sql,
158158
parameters: parameters,
159159
mapper: mapper
@@ -172,7 +172,7 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
172172
) -> AsyncStream<[RowType]> {
173173
AsyncStream { continuation in
174174
Task {
175-
for await values in self.kotlinDatabase.watch(
175+
for await values in try self.kotlinDatabase.watch(
176176
sql: sql,
177177
parameters: parameters,
178178
mapper: { cursor in

Sources/PowerSync/Kotlin/SqlCursor.swift

+20-20
Original file line numberDiff line numberDiff line change
@@ -8,60 +8,60 @@ extension SqlCursor {
88
}
99
return columnIndex
1010
}
11-
12-
private func getValue<T>(name: String, getter: (Int32) -> T?) throws -> T {
11+
12+
private func getValue<T>(name: String, getter: (Int32) throws -> T?) throws -> T {
1313
let columnIndex = try getColumnIndex(name: name)
14-
guard let value = getter(columnIndex) else {
14+
guard let value = try getter(columnIndex) else {
1515
throw SqlCursorError.nullValueFound(name)
1616
}
1717
return value
1818
}
19-
20-
private func getOptionalValue<T>(name: String, getter: (String) -> T?) throws -> T? {
19+
20+
private func getOptionalValue<T>(name: String, getter: (String) throws -> T?) throws -> T? {
2121
_ = try getColumnIndex(name: name)
22-
return getter(name)
22+
return try getter(name)
2323
}
24-
24+
2525
public func getBoolean(name: String) throws -> Bool {
2626
try getValue(name: name) { getBoolean(index: $0)?.boolValue }
2727
}
28-
28+
2929
public func getDouble(name: String) throws -> Double {
3030
try getValue(name: name) { getDouble(index: $0)?.doubleValue }
3131
}
32-
32+
3333
public func getLong(name: String) throws -> Int {
3434
try getValue(name: name) { getLong(index: $0)?.intValue }
3535
}
36-
36+
3737
public func getString(name: String) throws -> String {
3838
try getValue(name: name) { getString(index: $0) }
3939
}
40-
40+
4141
public func getBooleanOptional(name: String) throws -> Bool? {
42-
try getOptionalValue(name: name) { getBooleanOptional(name: $0)?.boolValue }
42+
try getOptionalValue(name: name) { try getBooleanOptional(name: $0)?.boolValue }
4343
}
44-
44+
4545
public func getDoubleOptional(name: String) throws -> Double? {
46-
try getOptionalValue(name: name) { getDoubleOptional(name: $0)?.doubleValue }
46+
try getOptionalValue(name: name) { try getDoubleOptional(name: $0)?.doubleValue }
4747
}
48-
48+
4949
public func getLongOptional(name: String) throws -> Int? {
50-
try getOptionalValue(name: name) { getLongOptional(name: $0)?.intValue }
50+
try getOptionalValue(name: name) { try getLongOptional(name: $0)?.intValue }
5151
}
52-
52+
5353
public func getStringOptional(name: String) throws -> String? {
54-
try getOptionalValue(name: name) { PowerSyncKotlin.SqlCursorKt.getStringOptional(self, name: $0) }
54+
try getOptionalValue(name: name) { try PowerSyncKotlin.SqlCursorKt.getStringOptional(self, name: $0) }
5555
}
5656
}
5757

5858
enum SqlCursorError: Error {
5959
case nullValue(message: String)
60-
60+
6161
static func columnNotFound(_ name: String) -> SqlCursorError {
6262
.nullValue(message: "Column '\(name)' not found")
6363
}
64-
64+
6565
static func nullValueFound(_ name: String) -> SqlCursorError {
6666
.nullValue(message: "Null value found for column \(name)")
6767
}

Sources/PowerSync/PowerSyncBackendConnectorAdapter.swift

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import OSLog
2+
13
class PowerSyncBackendConnectorAdapter: KotlinPowerSyncBackendConnector {
24
let swiftBackendConnector: PowerSyncBackendConnector
35

@@ -8,11 +10,29 @@ class PowerSyncBackendConnectorAdapter: KotlinPowerSyncBackendConnector {
810
}
911

1012
override func __fetchCredentials() async throws -> KotlinPowerSyncCredentials? {
11-
try await swiftBackendConnector.fetchCredentials()?.kotlinCredentials
13+
do {
14+
let result = try await swiftBackendConnector.fetchCredentials()
15+
return result?.kotlinCredentials
16+
} catch {
17+
if #available(iOS 14.0, *) {
18+
Logger().error("🔴 Failed to fetch credentials: \(error.localizedDescription)")
19+
} else {
20+
print("🔴 Failed to fetch credentials: \(error.localizedDescription)")
21+
}
22+
return nil
23+
}
1224
}
1325

1426
override func __uploadData(database: KotlinPowerSyncDatabase) async throws {
1527
let swiftDatabase = KotlinPowerSyncDatabaseImpl(kotlinDatabase: database)
16-
try await swiftBackendConnector.uploadData(database: swiftDatabase)
28+
do {
29+
return try await swiftBackendConnector.uploadData(database: swiftDatabase)
30+
} catch {
31+
if #available(iOS 14.0, *) {
32+
Logger().error("🔴 Failed to upload data: \(error)")
33+
} else {
34+
print("🔴 Failed to upload data: \(error)")
35+
}
36+
}
1737
}
1838
}

0 commit comments

Comments
 (0)