1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-26 03:58:44 +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
* New exception type `MessageIsNotModifierException` was added
* Now exceptions in requests will be caught correctly
## 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.call.HttpClientCall
import io.ktor.client.call.receive
import io.ktor.client.features.ClientRequestException
import io.ktor.client.response.readText
import kotlinx.coroutines.delay
import kotlinx.serialization.json.Json
@ -48,26 +50,36 @@ class KtorRequestsExecutor(
if (call == null) {
throw IllegalArgumentException("Can't execute request: $request")
}
val content = call.response.receive<String>()
val responseObject = jsonFormatter.parse(Response.serializer(), content)
try {
val content = call.response.receive<String>()
val responseObject = jsonFormatter.parse(Response.serializer(), content)
(responseObject.result ?.let {
jsonFormatter.fromJson(request.resultDeserializer, it)
} ?: responseObject.parameters ?.let {
val error = it.error
if (error is RetryAfterError) {
delay(error.leftToRetry)
execute(request)
} else {
null
}
} ?: call.let {
(responseObject.result?.let {
jsonFormatter.fromJson(request.resultDeserializer, it)
} ?: responseObject.parameters?.let {
val error = it.error
if (error is RetryAfterError) {
delay(error.leftToRetry)
execute(request)
} else {
null
}
} ?: 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(
responseObject,
content,
"Can't get result object from $content"
)
})
}
}
}