Improve error mapping

This commit is contained in:
Benoit Marty
2025-11-07 10:04:48 +01:00
parent a39b675cc1
commit 9e8fe55a90
4 changed files with 16 additions and 6 deletions

View File

@@ -32,8 +32,17 @@ sealed class ChangeServerError : Exception() {
companion object {
fun from(error: Throwable): ChangeServerError = when (error) {
is ChangeServerError -> error
is AuthenticationException.SlidingSyncVersion -> SlidingSyncAlert
is AuthenticationException.Oidc -> Error(messageStr = error.message)
is AuthenticationException -> {
when (error) {
is AuthenticationException.SlidingSyncVersion -> SlidingSyncAlert
is AuthenticationException.InvalidServerName,
is AuthenticationException.ServerUnreachable -> InvalidServer
// AccountAlreadyLoggedIn error should not happen at this point
is AuthenticationException.AccountAlreadyLoggedIn -> Error(messageStr = error.message)
is AuthenticationException.Generic -> Error(messageStr = error.message)
is AuthenticationException.Oidc -> Error(messageStr = error.message)
}
}
is AccountProviderAccessException.NeedElementProException -> NeedElementPro(
unauthorisedAccountProviderTitle = error.unauthorisedAccountProviderTitle,
applicationId = error.applicationId,

View File

@@ -14,6 +14,7 @@ sealed class AuthenticationException(message: String?) : Exception(message) {
class InvalidServerName(message: String?) : AuthenticationException(message)
class SlidingSyncVersion(message: String?) : AuthenticationException(message)
class ServerUnreachable(message: String?) : AuthenticationException(message)
class Oidc(message: String?) : AuthenticationException(message)
class Generic(message: String?) : AuthenticationException(message)
}

View File

@@ -19,7 +19,7 @@ fun Throwable.mapAuthenticationException(): AuthenticationException {
is ClientBuildException.InvalidServerName -> AuthenticationException.InvalidServerName(message)
is ClientBuildException.SlidingSyncVersion -> AuthenticationException.SlidingSyncVersion(message)
is ClientBuildException.Sdk -> AuthenticationException.Generic(message)
is ClientBuildException.ServerUnreachable -> AuthenticationException.Generic(message)
is ClientBuildException.ServerUnreachable -> AuthenticationException.ServerUnreachable(message)
is ClientBuildException.SlidingSync -> AuthenticationException.Generic(message)
is ClientBuildException.WellKnownDeserializationException -> AuthenticationException.Generic(message)
is ClientBuildException.WellKnownLookupFailed -> AuthenticationException.Generic(message)

View File

@@ -16,10 +16,10 @@ import org.matrix.rustcomponents.sdk.OidcException
class AuthenticationExceptionMappingTest {
@Test
fun `mapping an exception with no message returns 'Unknown error' message`() {
fun `mapping an exception with no message returns null message`() {
val exception = Exception()
val mappedException = exception.mapAuthenticationException()
assertThat(mappedException.message).isEqualTo("Unknown error")
assertThat(mappedException.message).isEqualTo(null)
}
@Test
@@ -46,7 +46,7 @@ class AuthenticationExceptionMappingTest {
assertThat(ClientBuildException.Sdk("SDK issue").mapAuthenticationException())
.isException<AuthenticationException.Generic>("SDK issue")
assertThat(ClientBuildException.ServerUnreachable("Server unreachable").mapAuthenticationException())
.isException<AuthenticationException.Generic>("Server unreachable")
.isException<AuthenticationException.ServerUnreachable>("Server unreachable")
assertThat(ClientBuildException.SlidingSync("Sliding Sync").mapAuthenticationException())
.isException<AuthenticationException.Generic>("Sliding Sync")
assertThat(ClientBuildException.WellKnownDeserializationException("WellKnown Deserialization").mapAuthenticationException())