此代码后引发以下异常。有趣的是,它在调试和逐步执行代码时有效。在最后一行抛出异常。
科特林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,byteArray.size)
val text = String(byteArray,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
如果不逐步使用调试器,则日志指出text
为空。怎么会这样?在所有内容都已传输之前运行吗?
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)