Improve error mapping

This commit is contained in:
Benoit Marty
2025-11-07 09:52:04 +01:00
parent a82c916ca9
commit a39b675cc1
6 changed files with 16 additions and 24 deletions

View File

@@ -10,13 +10,12 @@ package io.element.android.features.login.impl.changeserver
import androidx.compose.ui.tooling.preview.PreviewParameterProvider
import io.element.android.features.login.impl.error.ChangeServerError
import io.element.android.libraries.architecture.AsyncData
import io.element.android.libraries.ui.strings.CommonStrings
open class ChangeServerStateProvider : PreviewParameterProvider<ChangeServerState> {
override val values: Sequence<ChangeServerState>
get() = sequenceOf(
aChangeServerState(),
aChangeServerState(changeServerAction = AsyncData.Failure(ChangeServerError.Error(CommonStrings.error_unknown))),
aChangeServerState(changeServerAction = AsyncData.Failure(ChangeServerError.Error(null))),
aChangeServerState(changeServerAction = AsyncData.Failure(ChangeServerError.SlidingSyncAlert)),
aChangeServerState(
changeServerAction = AsyncData.Failure(

View File

@@ -26,6 +26,7 @@ import io.element.android.libraries.designsystem.components.dialogs.ErrorDialog
import io.element.android.libraries.designsystem.preview.ElementPreview
import io.element.android.libraries.designsystem.preview.PreviewsDayNight
import io.element.android.libraries.designsystem.theme.LocalBuildMeta
import io.element.android.libraries.ui.strings.CommonStrings
@Composable
fun ChangeServerView(
@@ -58,7 +59,7 @@ fun ChangeServerView(
is ChangeServerError.Error -> {
ErrorDialog(
modifier = modifier,
content = error.message(),
content = error.messageStr ?: stringResource(CommonStrings.error_unknown),
onSubmit = {
eventSink.invoke(ChangeServerEvents.ClearError)
}

View File

@@ -7,23 +7,13 @@
package io.element.android.features.login.impl.error
import androidx.annotation.StringRes
import androidx.compose.runtime.Composable
import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.ui.res.stringResource
import io.element.android.features.login.impl.changeserver.AccountProviderAccessException
import io.element.android.libraries.matrix.api.auth.AuthenticationException
import io.element.android.libraries.ui.strings.CommonStrings
sealed class ChangeServerError : Exception() {
data class Error(
@StringRes val messageId: Int? = null,
val messageStr: String? = null,
) : ChangeServerError() {
@Composable
@ReadOnlyComposable
fun message(): String = messageStr ?: stringResource(messageId ?: CommonStrings.error_unknown)
}
) : ChangeServerError()
data class NeedElementPro(
val unauthorisedAccountProviderTitle: String,
@@ -52,7 +42,7 @@ sealed class ChangeServerError : Exception() {
unauthorisedAccountProviderTitle = error.unauthorisedAccountProviderTitle,
authorisedAccountProviderTitles = error.authorisedAccountProviderTitles,
)
else -> InvalidServer
else -> Error(messageStr = error.message)
}
}
}

View File

@@ -54,7 +54,7 @@ fun LoginModeView(
}
is ChangeServerError.Error -> {
ErrorDialog(
content = error.message(),
content = error.messageStr ?: stringResource(CommonStrings.error_unknown),
onSubmit = onClearError,
)
}
@@ -102,7 +102,7 @@ fun LoginModeView(
}
is AuthenticationException.AccountAlreadyLoggedIn -> {
ErrorDialog(
content = stringResource(CommonStrings.error_account_already_logged_in, error.message.orEmpty()),
content = stringResource(CommonStrings.error_account_already_logged_in, error.userId),
onSubmit = onClearError,
)
}

View File

@@ -7,10 +7,13 @@
package io.element.android.libraries.matrix.api.auth
sealed class AuthenticationException(message: String) : Exception(message) {
class AccountAlreadyLoggedIn(userId: String) : AuthenticationException(userId)
class InvalidServerName(message: String) : AuthenticationException(message)
class SlidingSyncVersion(message: String) : AuthenticationException(message)
class Oidc(message: String) : AuthenticationException(message)
class Generic(message: String) : AuthenticationException(message)
sealed class AuthenticationException(message: String?) : Exception(message) {
data class AccountAlreadyLoggedIn(
val userId: String,
) : AuthenticationException(null)
class InvalidServerName(message: String?) : AuthenticationException(message)
class SlidingSyncVersion(message: String?) : AuthenticationException(message)
class Oidc(message: String?) : AuthenticationException(message)
class Generic(message: String?) : AuthenticationException(message)
}

View File

@@ -12,7 +12,6 @@ import org.matrix.rustcomponents.sdk.ClientBuildException
import org.matrix.rustcomponents.sdk.OidcException
fun Throwable.mapAuthenticationException(): AuthenticationException {
val message = this.message ?: "Unknown error"
return when (this) {
is AuthenticationException -> this
is ClientBuildException -> when (this) {