Stylianos Gakis
02/28/2024, 2:29 PMexpectSuccess = 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.Aleksei Tirman [JB]
02/28/2024, 2:51 PMexpectSuccess
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.Stylianos Gakis
02/28/2024, 3:09 PMStylianos Gakis
02/28/2024, 3:14 PMAleksei Tirman [JB]
02/28/2024, 3:36 PMNoTransformationFoundException
is thrown when the response body cannot be transformed into the specified type.Stylianos Gakis
02/28/2024, 3:37 PMresponse: 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?Aleksei Tirman [JB]
02/28/2024, 3:39 PMresponse: 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.Aleksei Tirman [JB]
02/28/2024, 3:41 PMIs 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.
Stylianos Gakis
02/28/2024, 3:41 PMsuspend 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?Stylianos Gakis
02/28/2024, 3:42 PMtry {
val myResponse: MyResponse = foo()
myResponse...
} catch (e: NoTransformationFoundException) {
e.response.statusCode // here I have the code
... here log, fallback to some error type, whatever
}
Aleksei Tirman [JB]
02/28/2024, 3:43 PMI should either be gettingYes, if theif it was a 200 and I manage to map it to the right type, and in all other scenarios I should be expecting aMyResponse
, is that not the case?NoTransformationFoundException
expectSuccess
is false
Stylianos Gakis
02/28/2024, 3:43 PMStylianos Gakis
02/28/2024, 3:43 PMStylianos Gakis
02/28/2024, 3:45 PMbody()
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
Aleksei Tirman [JB]
02/28/2024, 4:00 PMIOException
.Stylianos Gakis
02/28/2024, 4:01 PMAleksei Tirman [JB]
02/28/2024, 4:02 PMConnectException
may be thrown. The set of potential exception depend on the client engine.Aleksei Tirman [JB]
02/28/2024, 4:06 PMThrowable
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.
Stylianos Gakis
02/28/2024, 4:08 PMNoTransformationFoundException
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?Aleksei Tirman [JB]
02/28/2024, 4:39 PM