From b873898100dd5f77974817e1658fc22134ffa452 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 25 Aug 2022 13:35:15 +0600 Subject: [PATCH] wrap client request exception into exception handling --- .../tgbotapi/bot/ktor/KtorRequestsExecutor.kt | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/KtorRequestsExecutor.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/KtorRequestsExecutor.kt index 730924daa2..03ab290421 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/KtorRequestsExecutor.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/KtorRequestsExecutor.kt @@ -78,13 +78,18 @@ class KtorRequestsExecutor( pipelineStepsHolder.onRequestException(request, e) ?.let { return@let it } if (e is ClientRequestException) { - val content = e.response.bodyAsText() - val responseObject = jsonFormatter.decodeFromString(Response.serializer(), content) - newRequestException( - responseObject, - content, - "Can't get result object from $content" - ) + val exceptionResult = runCatchingSafely { + val content = e.response.bodyAsText() + val responseObject = jsonFormatter.decodeFromString(Response.serializer(), content) + newRequestException( + responseObject, + content, + "Can't get result object from $content" + ) + } + exceptionResult.exceptionOrNull() ?.let { + CommonBotException(cause = e) + } ?: exceptionResult.getOrThrow() } else { CommonBotException(cause = e) }