From 25fd71cb2c3517768a33ccde4d4ba6d7e10db1b4 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 12 Sep 2024 12:40:26 +0200 Subject: [PATCH] Make sure Throwable.mapAuthenticationException() is exhaustive on mapping ClientBuildException and add test for full coverage. --- .../matrix/impl/auth/AuthenticationException.kt | 15 +++++++++++---- .../auth/AuthenticationExceptionMappingTest.kt | 17 ++++++++++++++--- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/auth/AuthenticationException.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/auth/AuthenticationException.kt index 278b814fe2..cf48d68c70 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/auth/AuthenticationException.kt +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/auth/AuthenticationException.kt @@ -8,14 +8,21 @@ package io.element.android.libraries.matrix.impl.auth import io.element.android.libraries.matrix.api.auth.AuthenticationException -import org.matrix.rustcomponents.sdk.ClientBuildException as RustAuthenticationException +import org.matrix.rustcomponents.sdk.ClientBuildException fun Throwable.mapAuthenticationException(): AuthenticationException { val message = this.message ?: "Unknown error" return when (this) { - is RustAuthenticationException.Generic -> AuthenticationException.Generic(message) - is RustAuthenticationException.InvalidServerName -> AuthenticationException.InvalidServerName(message) - is RustAuthenticationException.SlidingSyncVersion -> AuthenticationException.SlidingSyncVersion(message) + is ClientBuildException -> when (this) { + is ClientBuildException.Generic -> AuthenticationException.Generic(message) + 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.SlidingSync -> AuthenticationException.Generic(message) + is ClientBuildException.WellKnownDeserializationException -> AuthenticationException.Generic(message) + is ClientBuildException.WellKnownLookupFailed -> AuthenticationException.Generic(message) + } else -> AuthenticationException.Generic(message) } } diff --git a/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/auth/AuthenticationExceptionMappingTest.kt b/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/auth/AuthenticationExceptionMappingTest.kt index fd007151c8..81a1667f4e 100644 --- a/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/auth/AuthenticationExceptionMappingTest.kt +++ b/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/auth/AuthenticationExceptionMappingTest.kt @@ -36,13 +36,24 @@ class AuthenticationExceptionMappingTest { assertThat(ClientBuildException.InvalidServerName("Invalid server name").mapAuthenticationException()) .isException("Invalid server name") - assertThat(ClientBuildException.Sdk("SDK issue").mapAuthenticationException()) - .isException("SDK issue") - assertThat(ClientBuildException.SlidingSyncVersion("Sliding sync not available").mapAuthenticationException()) .isException("Sliding sync not available") } + @Test + fun `mapping other exceptions map to the Generic Kotlin`() { + assertThat(ClientBuildException.Sdk("SDK issue").mapAuthenticationException()) + .isException("SDK issue") + assertThat(ClientBuildException.ServerUnreachable("Server unreachable").mapAuthenticationException()) + .isException("Server unreachable") + assertThat(ClientBuildException.SlidingSync("Sliding Sync").mapAuthenticationException()) + .isException("Sliding Sync") + assertThat(ClientBuildException.WellKnownDeserializationException("WellKnown Deserialization").mapAuthenticationException()) + .isException("WellKnown Deserialization") + assertThat(ClientBuildException.WellKnownLookupFailed("WellKnown Lookup Failed").mapAuthenticationException()) + .isException("WellKnown Lookup Failed") + } + private inline fun ThrowableSubject.isException(message: String) { isInstanceOf(T::class.java) hasMessageThat().isEqualTo(message)