mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2025-09-03 07:09:23 +00:00
fix of cancellation exception throwing?
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -9,6 +9,7 @@ settings.xml
|
|||||||
.gradle/
|
.gradle/
|
||||||
build/
|
build/
|
||||||
out/
|
out/
|
||||||
|
bin/
|
||||||
|
|
||||||
local.properties
|
local.properties
|
||||||
kotlin-js-store/
|
kotlin-js-store/
|
||||||
|
@@ -30491,6 +30491,11 @@ public final class dev/inmo/tgbotapi/utils/ByteReadChannelAllocatorDeserializati
|
|||||||
public fun getDescriptor ()Lkotlinx/serialization/descriptors/SerialDescriptor;
|
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 final class dev/inmo/tgbotapi/utils/DefaultKSLogKt {
|
||||||
public static final fun SetDefaultKTgBotAPIKSLog (ZLkotlin/jvm/functions/Function1;)V
|
public static final fun SetDefaultKTgBotAPIKSLog (ZLkotlin/jvm/functions/Function1;)V
|
||||||
public static synthetic fun SetDefaultKTgBotAPIKSLog$default (ZLkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
|
public static synthetic fun SetDefaultKTgBotAPIKSLog$default (ZLkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
package dev.inmo.tgbotapi.bot.ktor.base
|
package dev.inmo.tgbotapi.bot.ktor.base
|
||||||
|
|
||||||
import dev.inmo.kslog.common.*
|
import dev.inmo.kslog.common.*
|
||||||
|
import dev.inmo.micro_utils.coroutines.runCatchingLogging
|
||||||
import dev.inmo.tgbotapi.bot.BaseRequestsExecutor
|
import dev.inmo.tgbotapi.bot.BaseRequestsExecutor
|
||||||
import dev.inmo.tgbotapi.bot.exceptions.BotException
|
import dev.inmo.tgbotapi.bot.exceptions.BotException
|
||||||
import dev.inmo.tgbotapi.bot.exceptions.CommonBotException
|
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.*
|
||||||
import io.ktor.client.plugins.*
|
import io.ktor.client.plugins.*
|
||||||
import io.ktor.client.statement.*
|
import io.ktor.client.statement.*
|
||||||
|
import kotlinx.coroutines.CancellationException
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
|
|
||||||
class DefaultKtorRequestsExecutor internal constructor(
|
class DefaultKtorRequestsExecutor internal constructor(
|
||||||
@@ -47,7 +49,7 @@ class DefaultKtorRequestsExecutor internal constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun <T : Any> execute(request: Request<T>): T {
|
override suspend fun <T : Any> execute(request: Request<T>): T {
|
||||||
return runCatching {
|
return runCatchingLogging(logger = logger) {
|
||||||
logger.v { "Start request $request" }
|
logger.v { "Start request $request" }
|
||||||
pipelineStepsHolder.onBeforeSearchCallFactory(request, callsFactories)
|
pipelineStepsHolder.onBeforeSearchCallFactory(request, callsFactories)
|
||||||
requestsLimiter.limit(request) {
|
requestsLimiter.limit(request) {
|
||||||
@@ -95,6 +97,7 @@ class DefaultKtorRequestsExecutor internal constructor(
|
|||||||
CommonBotException(cause = e)
|
CommonBotException(cause = e)
|
||||||
} ?: exceptionResult.getOrThrow()
|
} ?: exceptionResult.getOrThrow()
|
||||||
}
|
}
|
||||||
|
is CancellationException,
|
||||||
is BotException -> e
|
is BotException -> e
|
||||||
else -> CommonBotException(cause = e)
|
else -> CommonBotException(cause = e)
|
||||||
}.also { newException ->
|
}.also { newException ->
|
||||||
|
@@ -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
|
@@ -19,7 +19,7 @@ private inline fun CreateDefaultKSLogger(
|
|||||||
): KSLog {
|
): KSLog {
|
||||||
val filter: MessageFilter? = if (dropCancellationExceptions) {
|
val filter: MessageFilter? = if (dropCancellationExceptions) {
|
||||||
{ ll, message, e ->
|
{ ll, message, e ->
|
||||||
e !is CancellationException
|
e ?.isCausedByCancellation() != true
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
null
|
null
|
||||||
|
@@ -15,6 +15,8 @@ import dev.inmo.tgbotapi.types.update.abstracts.BaseSentMessageUpdate
|
|||||||
import dev.inmo.tgbotapi.types.update.abstracts.Update
|
import dev.inmo.tgbotapi.types.update.abstracts.Update
|
||||||
import dev.inmo.tgbotapi.updateshandlers.*
|
import dev.inmo.tgbotapi.updateshandlers.*
|
||||||
import dev.inmo.tgbotapi.utils.DefaultKTgBotAPIKSLog
|
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 dev.inmo.tgbotapi.utils.subscribeWithBotLogger
|
||||||
import io.ktor.client.plugins.HttpRequestTimeoutException
|
import io.ktor.client.plugins.HttpRequestTimeoutException
|
||||||
import io.ktor.utils.io.CancellationException
|
import io.ktor.utils.io.CancellationException
|
||||||
@@ -106,7 +108,7 @@ fun TelegramBot.longPollingFlow(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.onFailure {
|
}.onFailure {
|
||||||
if (it is CancellationException) {
|
it.causedCancellationException() ?.let {
|
||||||
cancel(it)
|
cancel(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user