From d3633f6eead8465d95e4752771615d2842a6e317 Mon Sep 17 00:00:00 2001 From: Christian Melchior Date: Mon, 29 Jan 2024 08:49:25 +0100 Subject: [PATCH 1/3] Add specific exception for auth errors being sent to to the Sync Session error handler --- CHANGELOG.md | 4 +++- .../kotlin/mongodb/exceptions/SyncExceptions.kt | 12 ++++++++++++ .../realm/kotlin/mongodb/internal/RealmSyncUtils.kt | 2 ++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 74ef8c00d2..f8ca48b9f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,9 @@ ### Enhancements * [Sync] Added option to use managed WebSockets via OkHttp instead of Realm's built-in WebSocket client for Sync traffic (Only Android and JVM targets for now). Managed WebSockets offer improved support for proxies and firewalls that require authentication. This feature is currently opt-in and can be enabled by using `AppConfiguration.usePlatformNetworking()`. Managed WebSockets will become the default in a future version. (PR [#1528](https://github.com/realm/realm-kotlin/pull/1528)). -* `AutoClientResetFailed` exception now reports as the throwable cause any user exceptions that might occur during a client reset. (Issue [#1580](https://github.com/realm/realm-kotlin/issues/1580)) +* [Sync] `AutoClientResetFailed` exception now reports as the throwable cause any user exceptions that might occur during a client reset. (Issue [#1580](https://github.com/realm/realm-kotlin/issues/1580)) +* [Sync] Added a new `SyncSessionAuthExpired` exception subclass that will be sent to the `SyncConfiguration.errorHandler` if the sync session is stopped because the users refresh token +is no longer valid and they need to log in again. (Isse [#1640](https://github.com/realm/realm-kotlin/issues/1640)) ### Fixed * Cache notification callback JNI references at startup to ensure that symbols can be resolved in core callbacks. (Issue [#1577](https://github.com/realm/realm-kotlin/issues/1577)) diff --git a/packages/library-sync/src/commonMain/kotlin/io/realm/kotlin/mongodb/exceptions/SyncExceptions.kt b/packages/library-sync/src/commonMain/kotlin/io/realm/kotlin/mongodb/exceptions/SyncExceptions.kt index 2e6806062d..fdf7863f3e 100644 --- a/packages/library-sync/src/commonMain/kotlin/io/realm/kotlin/mongodb/exceptions/SyncExceptions.kt +++ b/packages/library-sync/src/commonMain/kotlin/io/realm/kotlin/mongodb/exceptions/SyncExceptions.kt @@ -63,6 +63,18 @@ public class WrongSyncTypeException internal constructor(message: String) : Sync public class BadFlexibleSyncQueryException internal constructor(message: String) : SyncException(message) +/** + * Thrown when the sync sessions access token expires and it isn't possible to renew it + * automatically because the users refresh token is no longer valid. + * + * Device Sync is stopped and can only be resumed after closing the realm, logging the user + * back in and reopen the realm by using the new user object. + * + * @see https://www.mongodb.com/docs/atlas/app-services/users/sessions/ + */ +public class SyncSessionAuthExpired internal constructor(message: String) : + SyncException(message) + /** * Thrown when the server undoes one or more client writes. Details on undone writes can be found in * [writes]. 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 fb72304c78..753dedf8dc 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 @@ -17,6 +17,7 @@ import io.realm.kotlin.mongodb.exceptions.FunctionExecutionException import io.realm.kotlin.mongodb.exceptions.InvalidCredentialsException import io.realm.kotlin.mongodb.exceptions.ServiceException import io.realm.kotlin.mongodb.exceptions.SyncException +import io.realm.kotlin.mongodb.exceptions.SyncSessionAuthExpired import io.realm.kotlin.mongodb.exceptions.UnrecoverableSyncException import io.realm.kotlin.mongodb.exceptions.UserAlreadyConfirmedException import io.realm.kotlin.mongodb.exceptions.UserAlreadyExistsException @@ -79,6 +80,7 @@ internal fun convertSyncError(syncError: SyncError): SyncException { val errorCode = syncError.errorCode val message = createMessageFromSyncError(errorCode) return when (errorCode.errorCode) { + ErrorCode.RLM_ERR_AUTH_ERROR -> SyncSessionAuthExpired(message) ErrorCode.RLM_ERR_WRONG_SYNC_TYPE -> WrongSyncTypeException(message) ErrorCode.RLM_ERR_INVALID_SUBSCRIPTION_QUERY -> { From 3f21532a72802e9c334b26390a0a3278b8304737 Mon Sep 17 00:00:00 2001 From: Christian Melchior Date: Mon, 29 Jan 2024 08:55:53 +0100 Subject: [PATCH 2/3] Line break --- CHANGELOG.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f8ca48b9f2..7fbc93f2a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,8 +6,7 @@ ### Enhancements * [Sync] Added option to use managed WebSockets via OkHttp instead of Realm's built-in WebSocket client for Sync traffic (Only Android and JVM targets for now). Managed WebSockets offer improved support for proxies and firewalls that require authentication. This feature is currently opt-in and can be enabled by using `AppConfiguration.usePlatformNetworking()`. Managed WebSockets will become the default in a future version. (PR [#1528](https://github.com/realm/realm-kotlin/pull/1528)). * [Sync] `AutoClientResetFailed` exception now reports as the throwable cause any user exceptions that might occur during a client reset. (Issue [#1580](https://github.com/realm/realm-kotlin/issues/1580)) -* [Sync] Added a new `SyncSessionAuthExpired` exception subclass that will be sent to the `SyncConfiguration.errorHandler` if the sync session is stopped because the users refresh token -is no longer valid and they need to log in again. (Isse [#1640](https://github.com/realm/realm-kotlin/issues/1640)) +* [Sync] Added a new `SyncSessionAuthExpired` exception subclass that will be sent to the `SyncConfiguration.errorHandler` if the sync session is stopped because the users refresh token is no longer valid and they need to log in again. (Isse [#1640](https://github.com/realm/realm-kotlin/issues/1640)) ### Fixed * Cache notification callback JNI references at startup to ensure that symbols can be resolved in core callbacks. (Issue [#1577](https://github.com/realm/realm-kotlin/issues/1577)) From 1459146d6776e7822831af491729b1d9bd42b4b6 Mon Sep 17 00:00:00 2001 From: Christian Melchior Date: Mon, 29 Jan 2024 09:01:06 +0100 Subject: [PATCH 3/3] Rename exception type --- .../io/realm/kotlin/mongodb/exceptions/SyncExceptions.kt | 2 +- .../kotlin/io/realm/kotlin/mongodb/internal/RealmSyncUtils.kt | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/library-sync/src/commonMain/kotlin/io/realm/kotlin/mongodb/exceptions/SyncExceptions.kt b/packages/library-sync/src/commonMain/kotlin/io/realm/kotlin/mongodb/exceptions/SyncExceptions.kt index fdf7863f3e..964cd1cf47 100644 --- a/packages/library-sync/src/commonMain/kotlin/io/realm/kotlin/mongodb/exceptions/SyncExceptions.kt +++ b/packages/library-sync/src/commonMain/kotlin/io/realm/kotlin/mongodb/exceptions/SyncExceptions.kt @@ -72,7 +72,7 @@ public class BadFlexibleSyncQueryException internal constructor(message: String) * * @see https://www.mongodb.com/docs/atlas/app-services/users/sessions/ */ -public class SyncSessionAuthExpired internal constructor(message: String) : +public class AuthExpiredException internal constructor(message: String) : SyncException(message) /** 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 753dedf8dc..5568b7ac77 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 @@ -8,6 +8,7 @@ import io.realm.kotlin.internal.interop.sync.AppError import io.realm.kotlin.internal.interop.sync.SyncError import io.realm.kotlin.mongodb.exceptions.AppException import io.realm.kotlin.mongodb.exceptions.AuthException +import io.realm.kotlin.mongodb.exceptions.AuthExpiredException import io.realm.kotlin.mongodb.exceptions.BadFlexibleSyncQueryException import io.realm.kotlin.mongodb.exceptions.BadRequestException import io.realm.kotlin.mongodb.exceptions.CompensatingWriteException @@ -17,7 +18,6 @@ import io.realm.kotlin.mongodb.exceptions.FunctionExecutionException import io.realm.kotlin.mongodb.exceptions.InvalidCredentialsException import io.realm.kotlin.mongodb.exceptions.ServiceException import io.realm.kotlin.mongodb.exceptions.SyncException -import io.realm.kotlin.mongodb.exceptions.SyncSessionAuthExpired import io.realm.kotlin.mongodb.exceptions.UnrecoverableSyncException import io.realm.kotlin.mongodb.exceptions.UserAlreadyConfirmedException import io.realm.kotlin.mongodb.exceptions.UserAlreadyExistsException @@ -80,7 +80,7 @@ internal fun convertSyncError(syncError: SyncError): SyncException { val errorCode = syncError.errorCode val message = createMessageFromSyncError(errorCode) return when (errorCode.errorCode) { - ErrorCode.RLM_ERR_AUTH_ERROR -> SyncSessionAuthExpired(message) + ErrorCode.RLM_ERR_AUTH_ERROR -> AuthExpiredException(message) ErrorCode.RLM_ERR_WRONG_SYNC_TYPE -> WrongSyncTypeException(message) ErrorCode.RLM_ERR_INVALID_SUBSCRIPTION_QUERY -> {