https://kotlinlang.org logo
#ktor
Title
# ktor
s

sindrenm

04/12/2022, 10:24 AM
Hi! Client question: Is `HttpResponseValidator`'s
handleResponseException
supposed to also catch `io.ktor.client.call.NoTransformationFoundException`s? Cause it just seems to fall through in my case. I can see it catching other non-HTTP exceptions such as `kotlinx.serialization.MissingFieldException`s, for instance, and of course `io.ktor.client.features.ClientRequestException`s and `io.ktor.client.features.ServerResponseException`s. I'm on Ktor v1.6.7 on Android using the
CIO
engine.
Here's the client config, if that's helpful. It's not overly complex.
Copy code
HttpClient(CIO) {
    install(Logging) {
        logger = Logger.ANDROID
        level = LogLevel.ALL
    }

    install(JsonFeature) {
        serializer = KotlinxSerializer(json)
    }

    HttpResponseValidator {
        handleResponseException {
            when (it) {
                // some specialized exception conversions ...
                // I would want/expect NoTransformationFoundExceptions as a potential case here
                is Exception -> throw ApiException.Other(it)
            }
        }
    }
}
I am able to force a
NoTransformationFoundException
by altering my
JsonFeature
installation:
Copy code
install(JsonFeature) {
    serializer = KotlinxSerializer(json)
    acceptContentTypes = listOf(ContentType("application", "pdf"))
}
This is obviously not right, but I would still expect it to end up in the
handleResponseException
block.
a

Aleksei Tirman [JB]

04/12/2022, 10:49 AM
The
NoTransformationFoundException
is thrown after executing the response pipeline but HttpResponseValidator’s exception handlers are called during execution of the response pipeline. So to catch it you can wrap a
receive()
call in
try/catch
.
s

sindrenm

04/12/2022, 10:55 AM
I see. And I guess this is the receive() you're talking about: https://api.ktor.io/older/1.6.7/ktor-client/ktor-client-core/io.ktor.client.call/r[…]=inline%20suspend%20fun%20%3CT%3E%20HttpResponse.receive():%20T Is there a way I can generalize this call inside of my engine config? I'd want this
try/catch
around all network calls, so the client config seems like the right place to put it.
I guess what I'm looking for is a way to get a hold of the
HttpResponse
in some kind of feature in the engine config.
a

Aleksei Tirman [JB]

04/12/2022, 12:15 PM
I don’t think it’s possible for
NoTransformationFoundException
s

sindrenm

04/12/2022, 12:38 PM
Gotcha. I also couldn't find anything. I'll look for another approach, then. Thanks a lot for the explanation, though! 😁
63 Views