I am using Ktor with kotlin multiplatform and I am trying to figure out why I am not getting any exceptions that get thrown. In my client configuration I am using the HttpResonseValidator to check the status code coming back
private val client = HttpClient(clientEngine) {
install(JsonFeature) {
serializer = KotlinxSerializer(Json.nonstrict)
}
// addDefaultResponseValidation()
HttpResponseValidator{
validateResponse { response: HttpResponse ->
val statusCode = response.status.value
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
}
}
}
I am returning http status code 401 error on my server for testing so I should see my code throw a ClientRequestException and the validateResponse does get called but I never see any exceptions in the console and my app just stops without any indication anything happened.
this is my call
private suspend fun getDataForUrl(url:String, callback: (MyObject?) -> Unit){
val response = client.get<MyObject>(url)
callback(response)
}
Which is called via
fun getData(callback: (MyObject?) -> Unit){
launch(dispatcher()){
getDataForUrl("$BASE_URL", callback)
}
}
When I surround the call with a try/catch
try{
val response = client.get<MyObject>(url)
catch(e:Exception){
}
I do get the exception but I dont really like that its getting caught here and not in the upper levels of my code.
Why is it getting swallowed up when there isn't a try/catch around it?