diff --git a/CHANGELOG.md b/CHANGELOG.md index f1f5313c5a..0dd323bb0c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## 28.0.2 +* `Core`: + * Fix of [#1001](https://github.com/InsanusMokrassar/ktgbotapi/issues/1001) + ## 28.0.1 * `Version`: diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/CausedByCancellation.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/CausedByCancellation.kt index 3b65f8071e..08c921dffc 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/CausedByCancellation.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/CausedByCancellation.kt @@ -2,6 +2,17 @@ package dev.inmo.tgbotapi.utils import kotlinx.coroutines.CancellationException +/** + * Traverses this throwable's cause chain to find a [CancellationException]. + * + * It walks through `this` and its `cause`s until the first [CancellationException] is encountered, + * and returns that exception. If no [CancellationException] is found in the chain, returns `null`. + * + * This is useful when you need to distinguish cancellations from other failures while handling errors. + * + * @receiver the root [Throwable] to inspect + * @return the first [CancellationException] found in the cause chain, or `null` if none present + */ fun Throwable.causedCancellationException(): CancellationException? { var current = this while (current !is CancellationException) { @@ -15,4 +26,10 @@ fun Throwable.causedCancellationException(): CancellationException? { return current } -fun Throwable.isCausedByCancellation(): Boolean = causedCancellationException() == null +/** + * Checks whether this throwable (or any of its causes) is a [CancellationException]. + * + * @receiver the [Throwable] to inspect + * @return `true` if a [CancellationException] is found in the cause chain, `false` otherwise + */ +fun Throwable.isCausedByCancellation(): Boolean = causedCancellationException() != null