Skip to content

Commit 6e127f1

Browse files
committed
Better logs for unknown requests
1 parent e428df3 commit 6e127f1

File tree

6 files changed

+25
-25
lines changed

6 files changed

+25
-25
lines changed

core/src/commonMain/kotlin/com/powersync/PowerSyncException.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ package com.powersync
22

33
public class PowerSyncException(
44
message: String,
5-
cause: Throwable,
5+
cause: Throwable?,
66
) : Exception(message, cause)

core/src/commonMain/kotlin/com/powersync/bucket/BucketStorageImpl.kt

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -184,18 +184,7 @@ internal class BucketStorageImpl(
184184
override suspend fun control(op: String, payload: ByteArray): List<Instruction> {
185185
return db.writeTransaction { tx ->
186186
logger.v { "powersync_control($op, binary payload)" }
187-
188-
try {
189-
tx.get("SELECT powersync_control(?, ?) AS r", listOf(op, payload), ::handleControlResult)
190-
} catch (e: Exception) {
191-
println("Got control exception, writing")
192-
SystemFileSystem.sink(Path("/Users/simon/failing_line.bin")).buffered().apply {
193-
write(payload)
194-
flush()
195-
close()
196-
}
197-
throw e
198-
}
187+
tx.get("SELECT powersync_control(?, ?) AS r", listOf(op, payload), ::handleControlResult)
199188
}
200189
}
201190
}

core/src/commonMain/kotlin/com/powersync/sync/Instruction.kt

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import kotlinx.serialization.encoding.CompositeDecoder
1313
import kotlinx.serialization.encoding.Decoder
1414
import kotlinx.serialization.encoding.Encoder
1515
import kotlinx.serialization.encoding.decodeStructure
16+
import kotlinx.serialization.json.JsonElement
1617
import kotlinx.serialization.json.JsonObject
1718
import kotlinx.serialization.serializer
1819

@@ -37,7 +38,7 @@ internal sealed interface Instruction {
3738
data object CloseSyncStream: Instruction
3839
data object DidCompleteSync: Instruction
3940

40-
data object UnknownInstruction: Instruction
41+
data class UnknownInstruction(val raw: JsonElement?): Instruction
4142

4243
class Serializer : KSerializer<Instruction> {
4344
private val logLine = serializer<LogLine>()
@@ -68,24 +69,25 @@ internal sealed interface Instruction {
6869
2 -> decodeSerializableElement(descriptor, 2, establishSyncStream)
6970
3 -> decodeSerializableElement(descriptor, 3, fetchCredentials)
7071
4 -> {
71-
decodeSerializableElement(descriptor, 3, flushFileSystem)
72+
decodeSerializableElement(descriptor, 4, flushFileSystem)
7273
FlushSileSystem
7374
}
7475
5 -> {
75-
decodeSerializableElement(descriptor, 4, closeSyncStream)
76+
decodeSerializableElement(descriptor, 5, closeSyncStream)
7677
CloseSyncStream
7778
}
7879
6 -> {
79-
decodeSerializableElement(descriptor, 4, didCompleteSync)
80+
decodeSerializableElement(descriptor, 6, didCompleteSync)
8081
DidCompleteSync
8182
}
82-
CompositeDecoder.UNKNOWN_NAME, CompositeDecoder.DECODE_DONE -> UnknownInstruction
83+
CompositeDecoder.UNKNOWN_NAME, -> UnknownInstruction(decodeSerializableElement(descriptor, index, serializer<JsonElement>()))
84+
CompositeDecoder.DECODE_DONE -> UnknownInstruction(null)
8385
else -> error("Unexpected index: $index")
8486
}
8587

8688
if (decodeElementIndex(descriptor) != CompositeDecoder.DECODE_DONE) {
8789
// Sync lines are single-key objects, make sure there isn't another one.
88-
UnknownInstruction
90+
UnknownInstruction(null)
8991
} else {
9092
value
9193
}
@@ -99,7 +101,7 @@ internal sealed interface Instruction {
99101
}
100102

101103
@Serializable
102-
internal class CoreSyncStatus(
104+
internal data class CoreSyncStatus(
103105
val connected: Boolean,
104106
val connecting: Boolean,
105107
val downloading: CoreDownloadProgress?,
@@ -108,12 +110,12 @@ internal class CoreSyncStatus(
108110
)
109111

110112
@Serializable
111-
internal class CoreDownloadProgress(
113+
internal data class CoreDownloadProgress(
112114
val buckets: Map<String, CoreBucketProgress>
113115
)
114116

115117
@Serializable
116-
internal class CoreBucketProgress(
118+
internal data class CoreBucketProgress(
117119
val priority: BucketPriority,
118120
@SerialName("at_last")
119121
val atLast: Long,
@@ -124,7 +126,7 @@ internal class CoreBucketProgress(
124126
)
125127

126128
@Serializable
127-
internal class CorePriorityStatus(
129+
internal data class CorePriorityStatus(
128130
val priority: BucketPriority,
129131
@SerialName("last_synced_at")
130132
@Serializable(with = InstantTimestampSerializer::class)

core/src/commonMain/kotlin/com/powersync/sync/RSocketSupport.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ internal fun HttpClient.rSocketSyncStream(
8383

8484
val rSocket = connector.connect(target)
8585
val syncStream = rSocket.requestStream(buildPayload {
86-
data("{}")
86+
data(JsonUtil.json.encodeToString(req))
8787
metadata(JsonUtil.json.encodeToString(RequestStreamMetadata("/sync/stream")))
8888
})
8989

core/src/commonMain/kotlin/com/powersync/sync/SyncStream.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.powersync.sync
33
import co.touchlab.kermit.Logger
44
import co.touchlab.kermit.Severity
55
import co.touchlab.stately.concurrency.AtomicBoolean
6+
import com.powersync.PowerSyncException
67
import com.powersync.bucket.BucketStorage
78
import com.powersync.bucket.WriteCheckpointResponse
89
import com.powersync.connectors.PowerSyncBackendConnector
@@ -337,7 +338,9 @@ internal class SyncStream(
337338
}
338339
is Instruction.FetchCredentials -> TODO()
339340
Instruction.DidCompleteSync -> status.update { copy(downloadError=null) }
340-
Instruction.UnknownInstruction -> TODO()
341+
is Instruction.UnknownInstruction -> {
342+
throw PowerSyncException("Unknown instruction received from core extension: ${instruction.raw}", null)
343+
}
341344
}
342345
}
343346

demos/supabase-todolist/desktopApp/src/jvmMain/kotlin/com/powersync/demos/Main.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@ import androidx.compose.ui.window.Window
77
import androidx.compose.ui.window.WindowPosition
88
import androidx.compose.ui.window.application
99
import androidx.compose.ui.window.rememberWindowState
10+
import co.touchlab.kermit.Logger
11+
import co.touchlab.kermit.Severity
12+
import co.touchlab.kermit.platformLogWriter
1013

1114

1215
fun main() {
16+
Logger.setLogWriters(platformLogWriter())
17+
Logger.setMinSeverity(Severity.Verbose)
18+
1319
application {
1420
Window(
1521
onCloseRequest = ::exitApplication,

0 commit comments

Comments
 (0)