Add SessionRestorationException, make sure ClientException can expose it through the cause property

This commit is contained in:
Jorge Martín 2025-12-04 11:05:19 +01:00 committed by Jorge Martin Espinosa
parent 33441d9d40
commit 10224d8e01
5 changed files with 26 additions and 9 deletions

View file

@ -0,0 +1,15 @@
/*
* Copyright (c) 2025 Element Creations Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
* Please see LICENSE files in the repository root for full details.
*/
package io.element.android.libraries.matrix.api.auth
import io.element.android.libraries.matrix.api.core.SessionId
sealed class SessionRestorationException(message: String, cause: Throwable? = null) : Exception(message, cause) {
data class MissingSession(val sessionId: SessionId) : SessionRestorationException("Session with id $sessionId not found")
class InvalidToken : SessionRestorationException("Access token is invalid or expired")
}

View file

@ -8,10 +8,10 @@
package io.element.android.libraries.matrix.api.exception
sealed class ClientException(message: String, val details: String?) : Exception(message) {
class Generic(message: String, details: String?) : ClientException(message, details)
class MatrixApi(val kind: ErrorKind, val code: String, message: String, details: String?) : ClientException(message, details)
class Other(message: String) : ClientException(message, null)
sealed class ClientException(message: String, val details: String?, cause: Throwable? = null) : Exception(message, cause) {
class Generic(message: String, details: String?, cause: Throwable? = null) : ClientException(message, details, cause)
class MatrixApi(val kind: ErrorKind, val code: String, message: String, details: String?, cause: Throwable? = null) : ClientException(message, details, cause)
class Other(message: String, cause: Throwable? = null) : ClientException(message, null, cause)
}
fun ClientException.isNetworkError(): Boolean {

View file

@ -20,6 +20,7 @@ import io.element.android.libraries.matrix.api.auth.MatrixAuthenticationService
import io.element.android.libraries.matrix.api.auth.MatrixHomeServerDetails
import io.element.android.libraries.matrix.api.auth.OidcDetails
import io.element.android.libraries.matrix.api.auth.OidcPrompt
import io.element.android.libraries.matrix.api.auth.SessionRestorationException
import io.element.android.libraries.matrix.api.auth.external.ExternalSession
import io.element.android.libraries.matrix.api.auth.qrlogin.MatrixQrCodeLoginData
import io.element.android.libraries.matrix.api.auth.qrlogin.QrCodeLoginStep
@ -91,10 +92,10 @@ class RustMatrixAuthenticationService(
}
rustMatrixClientFactory.create(sessionData)
} else {
error("Token is not valid")
throw SessionRestorationException.InvalidToken()
}
} else {
error("No session to restore with id $sessionId")
throw SessionRestorationException.MissingSession(sessionId)
}
}.mapFailure { failure ->
failure.mapClientException()

View file

@ -30,7 +30,7 @@ fun Throwable.mapRecoveryException(): RecoveryException {
}
}
else -> RecoveryException.Client(
ClientException.Other("Unknown error")
ClientException.Other("Unknown error", this)
)
}
}

View file

@ -15,15 +15,16 @@ fun Throwable.mapClientException(): ClientException {
return when (this) {
is RustClientException -> {
when (this) {
is RustClientException.Generic -> ClientException.Generic(msg, details)
is RustClientException.Generic -> ClientException.Generic(message = msg, details = details, cause = this)
is RustClientException.MatrixApi -> ClientException.MatrixApi(
kind = kind.map(),
code = code,
message = msg,
details = details,
cause = this,
)
}
}
else -> ClientException.Other(message ?: "Unknown error")
else -> ClientException.Other(message ?: "Unknown error", this)
}
}