Improve API of interface WellknownRetriever to be able to distinguish between 404 and other errors.
This commit is contained in:
@@ -286,7 +286,7 @@ class RustMatrixClient(
|
||||
override suspend fun getUrl(url: String): Result<ByteArray> = withContext(sessionDispatcher) {
|
||||
runCatchingExceptions {
|
||||
innerClient.getUrl(url)
|
||||
}
|
||||
}.mapFailure { it.mapClientException() }
|
||||
}
|
||||
|
||||
override suspend fun getRoom(roomId: RoomId): BaseRoom? = withContext(sessionDispatcher) {
|
||||
|
||||
@@ -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.di.SessionScope
|
||||
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.SessionWellknownRetriever
|
||||
import io.element.android.libraries.wellknown.api.WellKnown
|
||||
@@ -33,18 +34,9 @@ class DefaultSessionWellknownRetriever(
|
||||
.getUrl(url)
|
||||
.mapCatchingExceptions {
|
||||
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") }
|
||||
.map { it.map() }
|
||||
.fold(
|
||||
onSuccess = {
|
||||
WellknownRetrieverResult.Success(it)
|
||||
},
|
||||
onFailure = {
|
||||
WellknownRetrieverResult.Error(it as Exception)
|
||||
}
|
||||
)
|
||||
.toWellknownRetrieverResult()
|
||||
}
|
||||
|
||||
override suspend fun getElementWellKnown(): WellknownRetrieverResult<ElementWellKnown> {
|
||||
@@ -53,17 +45,23 @@ class DefaultSessionWellknownRetriever(
|
||||
.getUrl(url)
|
||||
.mapCatchingExceptions {
|
||||
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") }
|
||||
.map { it.map() }
|
||||
.fold(
|
||||
onSuccess = {
|
||||
WellknownRetrieverResult.Success(it)
|
||||
},
|
||||
onFailure = {
|
||||
WellknownRetrieverResult.Error(it as Exception)
|
||||
}
|
||||
)
|
||||
.toWellknownRetrieverResult()
|
||||
}
|
||||
|
||||
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