diff --git a/CHANGELOG.md b/CHANGELOG.md index a250b2a84f..a9e177d7da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ This release will bump the Realm file format 24. Opening a file with an older fo * Inserting the same typed link to the same key in a dictionary more than once would incorrectly create multiple backlinks to the object. This did not appear to cause any crashes later, but would have affecting explicit backlink count queries (eg: `...@links.@count`) and possibly notifications (Core Issue [realm/realm-core#7676](https://github.com/realm/realm-core/issues/7676) since v1.16.0). * [Sync] Automatic client reset recovery would crash when recovering AddInteger instructions on a Mixed property if its type was changed to non-integer (Core issue [realm/realm-core#7683](https://github.com/realm/realm-core/pull/7683), since v0.11.0). * [Sync] Typos [SubscriptionSetState.SUPERSEDED], [SyncTimeoutOptions.pingKeepalivePeriod] and [SyncTimeoutOptions.pongKeepalivePeriod]. (Issue [#1754](https://github.com/realm/realm-kotlin/pull/1754) +* [Sync] Fatal sync exceptions are now thrown as `UnrecoverableSyncException`. (Issue [#1767](https://github.com/realm/realm-kotlin/issues/1767) [RKOTLIN-1096](https://jira.mongodb.org/browse/RKOTLIN-1096)). ### Compatibility * File format: Generates Realms with file format v24 (reads and upgrades file format v10 or later). diff --git a/packages/library-sync/src/commonMain/kotlin/io/realm/kotlin/mongodb/internal/RealmSyncUtils.kt b/packages/library-sync/src/commonMain/kotlin/io/realm/kotlin/mongodb/internal/RealmSyncUtils.kt index 73fb222306..8985b87757 100644 --- a/packages/library-sync/src/commonMain/kotlin/io/realm/kotlin/mongodb/internal/RealmSyncUtils.kt +++ b/packages/library-sync/src/commonMain/kotlin/io/realm/kotlin/mongodb/internal/RealmSyncUtils.kt @@ -78,26 +78,27 @@ internal fun channelResultCallback( internal fun convertSyncError(syncError: SyncError): SyncException { val errorCode = syncError.errorCode val message = createMessageFromSyncError(errorCode) - return when (errorCode.errorCode) { - ErrorCode.RLM_ERR_WRONG_SYNC_TYPE -> WrongSyncTypeException(message) + return if (syncError.isFatal) { + // An unrecoverable exception happened + UnrecoverableSyncException(message) + } else { + when (errorCode.errorCode) { + ErrorCode.RLM_ERR_WRONG_SYNC_TYPE -> WrongSyncTypeException(message) - ErrorCode.RLM_ERR_INVALID_SUBSCRIPTION_QUERY -> { - // Flexible Sync Query was rejected by the server - BadFlexibleSyncQueryException(message) - } - ErrorCode.RLM_ERR_SYNC_COMPENSATING_WRITE -> CompensatingWriteException(message, syncError.compensatingWrites) + ErrorCode.RLM_ERR_INVALID_SUBSCRIPTION_QUERY -> { + // Flexible Sync Query was rejected by the server + BadFlexibleSyncQueryException(message) + } - ErrorCode.RLM_ERR_SYNC_PROTOCOL_INVARIANT_FAILED, - ErrorCode.RLM_ERR_SYNC_PROTOCOL_NEGOTIATION_FAILED, - ErrorCode.RLM_ERR_SYNC_PERMISSION_DENIED -> { - // Permission denied errors should be unrecoverable according to Core, i.e. the - // client will disconnect sync and transition to the "inactive" state - UnrecoverableSyncException(message) - } - else -> { - // An error happened we are not sure how to handle. Just report as a generic - // SyncException. - SyncException(message) + ErrorCode.RLM_ERR_SYNC_COMPENSATING_WRITE -> CompensatingWriteException( + message, + syncError.compensatingWrites + ) + else -> { + // An error happened we are not sure how to handle. Just report as a generic + // SyncException. + SyncException(message) + } } } } diff --git a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SyncedRealmTests.kt b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SyncedRealmTests.kt index 51f3c65f3a..b2ff132e1e 100644 --- a/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SyncedRealmTests.kt +++ b/packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/SyncedRealmTests.kt @@ -36,7 +36,6 @@ import io.realm.kotlin.mongodb.App import io.realm.kotlin.mongodb.User import io.realm.kotlin.mongodb.exceptions.DownloadingRealmTimeOutException import io.realm.kotlin.mongodb.exceptions.SyncException -import io.realm.kotlin.mongodb.exceptions.UnrecoverableSyncException import io.realm.kotlin.mongodb.subscriptions import io.realm.kotlin.mongodb.sync.InitialSubscriptionsCallback import io.realm.kotlin.mongodb.sync.SubscriptionSetState @@ -336,13 +335,12 @@ class SyncedRealmTests { Realm.open(config).use { // Make sure that the test eventually fail. Coroutines can cancel a delay // so this doesn't always block the test for 10 seconds. - delay(10 * 1000) + delay(10_000) channel.send(AssertionError("Realm was successfully opened")) } } val error = channel.receiveOrFail() - assertTrue(error is UnrecoverableSyncException, "Was $error") val message = error.message assertNotNull(message) assertTrue(