https://kotlinlang.org logo
Title
j

Joe Altidore

08/07/2022, 2:11 PM
Hello guys. I am using ktor client and I can't find a way to handle response error.
a

Aleksei Tirman [JB]

08/08/2022, 11:45 AM
Hey. What do you mean by a response error? If you mean some network error then you can wrap a client’s request call in
try/catch
block to handle the error.
j

Joe Altidore

08/08/2022, 12:21 PM
@Aleksei Tirman [JB] I have introduced the try catch block but I want to know how to parse the error message returned by the API. Bringing it to context, I wrote the server side code and ensured that if any error occurs, a uniform error object is returned as response: data class Error( val status: Boolean = false, val message: String ) How do I have access to this object from the catch block?
Secondly, is there no way I can do that in the HttpClient configuration rather than repeating it in every endpoint?
a

Aleksei Tirman [JB]

08/08/2022, 12:29 PM
What you are describing is not a server error so you can just deserialize a response to your data class using the ContentNegotiation plugin.
j

Joe Altidore

08/08/2022, 1:12 PM
I have used ContentNegotiation on the serverside, how do I use it with an Android client?
a

Aleksei Tirman [JB]

08/08/2022, 1:12 PM
j

Joe Altidore

08/08/2022, 1:24 PM
Thanks