The exception below is thrown after this code. Interestingly it works while debugging and stepping through the code. The exception is thrown on the last line.
Kotlin 1.3.60
Ktor 1.2.6
kotlinx.serialization 0.14.0
} catch (throwable: Throwable) {
when (throwable) {
is ClientRequestException -> {
val statusCode = throwable.response.status.value
val byteArray = ByteArray(throwable.response.content.availableForRead)
throwable.response.content.readFully(byteArray, 0, byteArray.size)
val text = String(byteArray, 0, byteArray.size, Charsets.UTF_8)
logger.log("text is: $text")
val isJson = throwable.response.headers["Content-Type"]?.startsWith("application/json") ?: false
val failure = if (isJson) {
val json = Json(JsonConfiguration.Default).parseJson(text).jsonObject
When not stepping through with the debugger, the log states that text
is empty. How can this be? Is this running before all the content has been transferred?
kotlinx.serialization.json.JsonDecodingException: Invalid JSON at 0: Can't begin reading from here
at kotlinx.serialization.json.internal.JsonReader.fail(JsonReader.kt:293)
at kotlinx.serialization.json.internal.JsonReader.fail$default(JsonReader.kt:292)