https://kotlinlang.org logo
#server
Title
# server
k

Kristian Nedrevold

08/04/2022, 12:46 PM
I am using ktor with ContentNegotiation. What is the idiomatic way to handle the unchecked exceptions you can get when retrieving a response using ktor client? example code
val responseData: ReservedCitizenResponse = response.body()
. If there is something wrong in the response body this will just crash with an JsonDecodingException as an unchecked exception. It would be nice if the .body() function returned a Result monad so you could handle the cases of successful and unsuccessful serialization.
val responseData: ReservedCitizenResponse = try {response.body() } catch (e: Exception) { return Result.failure(NHNInvalidResponseBodyException("Json response does not match expected fields\ne${e.message}")) }
This is how I have solved it for now, which is not very elegant, but does allow me to return early.
👎 1
l

Landry Norris

08/04/2022, 2:15 PM
You can use StatusPages if you want to return a generic error.
3 Views