1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-12-01 22:40:07 +00:00

fix of exceptions catching

This commit is contained in:
InsanusMokrassar 2019-12-13 23:54:02 +06:00
parent 1659f6f909
commit ac915b79f7
2 changed files with 27 additions and 14 deletions

View File

@ -25,6 +25,7 @@
### 0.20.2 ### 0.20.2
* New exception type `MessageIsNotModifierException` was added * New exception type `MessageIsNotModifierException` was added
* Now exceptions in requests will be caught correctly
## 0.19.0 ImplicitReflection removing ## 0.19.0 ImplicitReflection removing

View File

@ -13,6 +13,8 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.TelegramAPIUrlsKeeper
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import io.ktor.client.call.HttpClientCall import io.ktor.client.call.HttpClientCall
import io.ktor.client.call.receive import io.ktor.client.call.receive
import io.ktor.client.features.ClientRequestException
import io.ktor.client.response.readText
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
@ -48,26 +50,36 @@ class KtorRequestsExecutor(
if (call == null) { if (call == null) {
throw IllegalArgumentException("Can't execute request: $request") throw IllegalArgumentException("Can't execute request: $request")
} }
val content = call.response.receive<String>() try {
val responseObject = jsonFormatter.parse(Response.serializer(), content) val content = call.response.receive<String>()
val responseObject = jsonFormatter.parse(Response.serializer(), content)
(responseObject.result ?.let { (responseObject.result?.let {
jsonFormatter.fromJson(request.resultDeserializer, it) jsonFormatter.fromJson(request.resultDeserializer, it)
} ?: responseObject.parameters ?.let { } ?: responseObject.parameters?.let {
val error = it.error val error = it.error
if (error is RetryAfterError) { if (error is RetryAfterError) {
delay(error.leftToRetry) delay(error.leftToRetry)
execute(request) execute(request)
} else { } else {
null null
} }
} ?: call.let { } ?: call.let {
throw newRequestException(
responseObject,
content,
"Can't get result object from $content"
)
})
} catch (e: ClientRequestException) {
val content = e.response.readText()
val responseObject = jsonFormatter.parse(Response.serializer(), content)
throw newRequestException( throw newRequestException(
responseObject, responseObject,
content, content,
"Can't get result object from $content" "Can't get result object from $content"
) )
}) }
} }
} }