I Just started to explore KMM, which seems really nice so far.
Basically, what I'm trying to to is to handle all http error code (like 401, 404, 500) globally, in one place.
However, I'm not sure how to interact with the response.
I installed ResponseObserver in HttpClient, but it only goes there if http status is 200.
I also tried to use HttpResponseValidator, but it never goes there.
Here's the code:
class ApiImpl : Api {
private val httpClient = HttpClient {
install(JsonFeature) {
val json = kotlinx.serialization.json.Json { ignoreUnknownKeys = true }
serializer = KotlinxSerializer(json)
}
install(ResponseObserver) {
onResponse { response ->
//*** We get here only if http status code is 200 ***/
println("HTTP status: ${response.status.value}")
}
}
HttpResponseValidator {
validateResponse { response: HttpResponse ->
val statusCode = response.status.value
//*** We never get here ***/
println("HTTP status: $statusCode")
when (statusCode) {
in 300..399 -> throw RedirectResponseException(response)
in 400..499 -> throw ClientRequestException(response)
in 500..599 -> throw ServerResponseException(response)
}
if (statusCode >= 600) {
throw ResponseException(response)
}
}
handleResponseException { cause: Throwable ->
throw cause
}
}
}
override suspend fun getUsers(): List<User> {
try {
return httpClient.get("some url....")
} catch (e: Exception) {
//*** We get here in case of 404 (for example), but I don't want to repeat it in every request ***/
println(e.message)
}
return emptyList()
}
}



