mirror of
https://github.com/Novaloop-AG/element-x-android.git
synced 2026-11-02 12:07:28 +00:00
Improve API of interface WellknownRetriever to be able to distinguish between 404 and other errors.
This commit is contained in:
+1
-1
@@ -286,7 +286,7 @@ class RustMatrixClient(
|
|||||||
override suspend fun getUrl(url: String): Result<ByteArray> = withContext(sessionDispatcher) {
|
override suspend fun getUrl(url: String): Result<ByteArray> = withContext(sessionDispatcher) {
|
||||||
runCatchingExceptions {
|
runCatchingExceptions {
|
||||||
innerClient.getUrl(url)
|
innerClient.getUrl(url)
|
||||||
}
|
}.mapFailure { it.mapClientException() }
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun getRoom(roomId: RoomId): BaseRoom? = withContext(sessionDispatcher) {
|
override suspend fun getRoom(roomId: RoomId): BaseRoom? = withContext(sessionDispatcher) {
|
||||||
|
|||||||
+20
-22
@@ -13,6 +13,7 @@ import io.element.android.libraries.androidutils.json.JsonProvider
|
|||||||
import io.element.android.libraries.core.extensions.mapCatchingExceptions
|
import io.element.android.libraries.core.extensions.mapCatchingExceptions
|
||||||
import io.element.android.libraries.di.SessionScope
|
import io.element.android.libraries.di.SessionScope
|
||||||
import io.element.android.libraries.matrix.api.MatrixClient
|
import io.element.android.libraries.matrix.api.MatrixClient
|
||||||
|
import io.element.android.libraries.matrix.api.exception.ClientException
|
||||||
import io.element.android.libraries.wellknown.api.ElementWellKnown
|
import io.element.android.libraries.wellknown.api.ElementWellKnown
|
||||||
import io.element.android.libraries.wellknown.api.SessionWellknownRetriever
|
import io.element.android.libraries.wellknown.api.SessionWellknownRetriever
|
||||||
import io.element.android.libraries.wellknown.api.WellKnown
|
import io.element.android.libraries.wellknown.api.WellKnown
|
||||||
@@ -33,18 +34,9 @@ class DefaultSessionWellknownRetriever(
|
|||||||
.getUrl(url)
|
.getUrl(url)
|
||||||
.mapCatchingExceptions {
|
.mapCatchingExceptions {
|
||||||
val data = String(it)
|
val data = String(it)
|
||||||
json().decodeFromString(InternalWellKnown.serializer(), data)
|
json().decodeFromString<InternalWellKnown>(data).map()
|
||||||
}
|
}
|
||||||
.onFailure { Timber.e(it, "Failed to retrieve .well-known from $domain") }
|
.toWellknownRetrieverResult()
|
||||||
.map { it.map() }
|
|
||||||
.fold(
|
|
||||||
onSuccess = {
|
|
||||||
WellknownRetrieverResult.Success(it)
|
|
||||||
},
|
|
||||||
onFailure = {
|
|
||||||
WellknownRetrieverResult.Error(it as Exception)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun getElementWellKnown(): WellknownRetrieverResult<ElementWellKnown> {
|
override suspend fun getElementWellKnown(): WellknownRetrieverResult<ElementWellKnown> {
|
||||||
@@ -53,17 +45,23 @@ class DefaultSessionWellknownRetriever(
|
|||||||
.getUrl(url)
|
.getUrl(url)
|
||||||
.mapCatchingExceptions {
|
.mapCatchingExceptions {
|
||||||
val data = String(it)
|
val data = String(it)
|
||||||
json().decodeFromString(InternalElementWellKnown.serializer(), data)
|
json().decodeFromString<InternalElementWellKnown>(data).map()
|
||||||
}
|
}
|
||||||
.onFailure { Timber.e(it, "Failed to retrieve Element .well-known from $domain") }
|
.toWellknownRetrieverResult()
|
||||||
.map { it.map() }
|
|
||||||
.fold(
|
|
||||||
onSuccess = {
|
|
||||||
WellknownRetrieverResult.Success(it)
|
|
||||||
},
|
|
||||||
onFailure = {
|
|
||||||
WellknownRetrieverResult.Error(it as Exception)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun <T> Result<T>.toWellknownRetrieverResult(): WellknownRetrieverResult<T> = fold(
|
||||||
|
onSuccess = {
|
||||||
|
WellknownRetrieverResult.Success(it)
|
||||||
|
},
|
||||||
|
onFailure = {
|
||||||
|
Timber.e(it, "Failed to retrieve Element .well-known from $domain")
|
||||||
|
// This check on message value is not ideal but this is what we got from the SDK.
|
||||||
|
if ((it as? ClientException.Generic)?.message?.contains("404") == true) {
|
||||||
|
WellknownRetrieverResult.NotFound
|
||||||
|
} else {
|
||||||
|
WellknownRetrieverResult.Error(it as Exception)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user