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

Stylianos Gakis

02/28/2024, 2:29 PM
Reading the docs for “Response validation” for ktor client, it implies that using
expectSuccess = true
will mean that all non 200 responses will throw their respective error. If however one does not add
expectSuccess
, and then they just try to do
response.body<SomeType>()
directly, what is the exception they are supposed to be receiving? Will it simply throw an
NoTransformationFoundException
directly, since it failed to transform to that new type? Since in a non 200 response it anyway had 0 chance it would’ve been transformed to our type anyway.
a

Aleksei Tirman [JB]

02/28/2024, 2:51 PM
When
expectSuccess
is set to
true
and the status code indicates an error, the exception is thrown when retrieving a response from the server, occurring before the response body is received. In other words, the exception is thrown before the
body
method is called.
s

Stylianos Gakis

02/28/2024, 3:09 PM
Right, sorry I probably wasn't clear about my question. When expectSuccess is set it makes sense. When it is not set I wonder what is the behavior.
I went on these docs trying to find out about the behavior without expectSuccess and I feel like I haven't gotten my answer. Perhaps there is another page I should be looking at instead?
a

Aleksei Tirman [JB]

02/28/2024, 3:36 PM
As you correctly guessed, the
NoTransformationFoundException
is thrown when the response body cannot be transformed into the specified type.
s

Stylianos Gakis

02/28/2024, 3:37 PM
Alright great, I was just missing some reassurance on the docs basically. Thanks a lot for the help here! And then I guess inside the exception itself I can take the
response: HttpResponse
to log the status code and whatever, so this should work for me! Is it that I am missing something in the docs btw around this, or could there be some addition to mention that scenario?
a

Aleksei Tirman [JB]

02/28/2024, 3:39 PM
> And then I guess inside the exception itself I can take the
response: HttpResponse
to log the status code and whatever, so this should work for me! No, you would already have the
HttpResponse
object because the
NoTransformationFoundException
may be thrown when calling the
body
method.
Is it that I am missing something in the docs btw around this, or could there be some addition to mention that scenario?
I think this information is missing in the docs.
s

Stylianos Gakis

02/28/2024, 3:41 PM
Right, but since my function looks smth like this:
Copy code
suspend fun foo(): MyResponse {
    val response =  <http://ktorClient.post|ktorClient.post>("...") {
        contentType(ContentType.Application.Json)
        setBody(...)
    }
    return response.body<MyResponse>()
}
I should either be getting
MyResponse
if it was a 200 and I manage to map it to the right type, and in all other scenarios I should be expecting a
NoTransformationFoundException
, is that not the case? What else should I be catching here instead?
Call site would look like
Copy code
try {
  val myResponse: MyResponse = foo()
  myResponse...
} catch (e: NoTransformationFoundException) {
  e.response.statusCode // here I have the code
  ... here log, fallback to some error type, whatever
}
a

Aleksei Tirman [JB]

02/28/2024, 3:43 PM
I should either be getting
MyResponse
if it was a 200 and I manage to map it to the right type, and in all other scenarios I should be expecting a
NoTransformationFoundException
, is that not the case?
Yes, if the
expectSuccess
is
false
s

Stylianos Gakis

02/28/2024, 3:43 PM
Yeah I am planning to keep it false indeed
It’s already false and I do not want to flip it and have other consequences in places I do not expect, hence me trying make sure I am not missing something here
I see that
body()
also might throw
DoubleReceiveException
, but with this approach I really don’t think I need to catch that. And from my understanding I also don’t need to be catching anything like
IOException
etc since I do not see that I am calling any function that indicates that it would throw it. I only call
<http://HttpClient.post|HttpClient.post>
and then
HttpResponse.body
a

Aleksei Tirman [JB]

02/28/2024, 4:00 PM
Those methods could throw the
IOException
.
s

Stylianos Gakis

02/28/2024, 4:01 PM
That's not documented in the documentation above the function 😬 Okay I feel like we need to take a step back. How do I make sure when using ktor client that I don't just crash my app by trying to do a normal request? Are there some docs that just cover this in its entirety without me having to be worried or having to ask you? 😕
a

Aleksei Tirman [JB]

02/28/2024, 4:02 PM
Also, the
ConnectException
may be thrown. The set of potential exception depend on the client engine.
If you don't want your app to crash, you should catch every exception (the
Throwable
type). Also, there's a feature request to group all Ktor exceptions into one type."
Are there some docs that just cover this in its entirety without me having to be worried or having to ask you?
Unfortunately, there is no such docs.
s

Stylianos Gakis

02/28/2024, 4:08 PM
Okay so I think I got this: I catch first
NoTransformationFoundException
and I log more specifically from the information I have here. I then catch Throwable, and log that as a more generic error. I also make sure in the catch that catches
Throwable
that I rethrow specifically
CancellationException
so as not to break structured concurrency. This plan should cover me 100% I think. How does it sound?
a

Aleksei Tirman [JB]

02/28/2024, 4:39 PM
Sounds good.
thank you color 1
13 Views