1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2025-09-02 22:59:48 +00:00

fix of cancellation exception throwing?

This commit is contained in:
2025-07-21 20:41:51 +06:00
parent 3bd4fab3fe
commit 2ba447b126
6 changed files with 32 additions and 3 deletions

1
.gitignore vendored
View File

@@ -9,6 +9,7 @@ settings.xml
.gradle/
build/
out/
bin/
local.properties
kotlin-js-store/

View File

@@ -30491,6 +30491,11 @@ public final class dev/inmo/tgbotapi/utils/ByteReadChannelAllocatorDeserializati
public fun getDescriptor ()Lkotlinx/serialization/descriptors/SerialDescriptor;
}
public final class dev/inmo/tgbotapi/utils/CausedByCancellationKt {
public static final fun causedCancellationException (Ljava/lang/Throwable;)Ljava/util/concurrent/CancellationException;
public static final fun isCausedByCancellation (Ljava/lang/Throwable;)Z
}
public final class dev/inmo/tgbotapi/utils/DefaultKSLogKt {
public static final fun SetDefaultKTgBotAPIKSLog (ZLkotlin/jvm/functions/Function1;)V
public static synthetic fun SetDefaultKTgBotAPIKSLog$default (ZLkotlin/jvm/functions/Function1;ILjava/lang/Object;)V

View File

@@ -1,6 +1,7 @@
package dev.inmo.tgbotapi.bot.ktor.base
import dev.inmo.kslog.common.*
import dev.inmo.micro_utils.coroutines.runCatchingLogging
import dev.inmo.tgbotapi.bot.BaseRequestsExecutor
import dev.inmo.tgbotapi.bot.exceptions.BotException
import dev.inmo.tgbotapi.bot.exceptions.CommonBotException
@@ -16,6 +17,7 @@ import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper
import io.ktor.client.*
import io.ktor.client.plugins.*
import io.ktor.client.statement.*
import kotlinx.coroutines.CancellationException
import kotlinx.serialization.json.Json
class DefaultKtorRequestsExecutor internal constructor(
@@ -47,7 +49,7 @@ class DefaultKtorRequestsExecutor internal constructor(
}
override suspend fun <T : Any> execute(request: Request<T>): T {
return runCatching {
return runCatchingLogging(logger = logger) {
logger.v { "Start request $request" }
pipelineStepsHolder.onBeforeSearchCallFactory(request, callsFactories)
requestsLimiter.limit(request) {
@@ -95,6 +97,7 @@ class DefaultKtorRequestsExecutor internal constructor(
CommonBotException(cause = e)
} ?: exceptionResult.getOrThrow()
}
is CancellationException,
is BotException -> e
else -> CommonBotException(cause = e)
}.also { newException ->

View File

@@ -0,0 +1,18 @@
package dev.inmo.tgbotapi.utils
import kotlinx.coroutines.CancellationException
fun Throwable.causedCancellationException(): CancellationException? {
var current = this
while (current !is CancellationException) {
when {
// It is possible, that API will be changed and cancellation will be caused by something else
current is CancellationException && current.cause == null -> return current
else -> current = current.cause ?: return null
}
}
return current
}
fun Throwable.isCausedByCancellation(): Boolean = causedCancellationException() == null

View File

@@ -19,7 +19,7 @@ private inline fun CreateDefaultKSLogger(
): KSLog {
val filter: MessageFilter? = if (dropCancellationExceptions) {
{ ll, message, e ->
e !is CancellationException
e ?.isCausedByCancellation() != true
}
} else {
null

View File

@@ -15,6 +15,8 @@ import dev.inmo.tgbotapi.types.update.abstracts.BaseSentMessageUpdate
import dev.inmo.tgbotapi.types.update.abstracts.Update
import dev.inmo.tgbotapi.updateshandlers.*
import dev.inmo.tgbotapi.utils.DefaultKTgBotAPIKSLog
import dev.inmo.tgbotapi.utils.causedCancellationException
import dev.inmo.tgbotapi.utils.isCausedByCancellation
import dev.inmo.tgbotapi.utils.subscribeWithBotLogger
import io.ktor.client.plugins.HttpRequestTimeoutException
import io.ktor.utils.io.CancellationException
@@ -106,7 +108,7 @@ fun TelegramBot.longPollingFlow(
}
}
}.onFailure {
if (it is CancellationException) {
it.causedCancellationException() ?.let {
cancel(it)
}
}