Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add specific exception for auth errors being sent to to the Sync Session error handler #1643

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

### 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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 AuthExpiredException 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].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 -> AuthExpiredException(message)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also send this to the AuthenticationListener

Copy link
Contributor

@rorbech rorbech Mar 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also send this to the AuthenticationListener

We should probably update the AuthenticationListener to take advantage of realm/realm-core#7302 instead of issuing these events ourselves.

Would also be fixed by #1676

ErrorCode.RLM_ERR_WRONG_SYNC_TYPE -> WrongSyncTypeException(message)

ErrorCode.RLM_ERR_INVALID_SUBSCRIPTION_QUERY -> {
Expand Down
Loading