1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2025-09-15 13:19:30 +00:00
This commit is contained in:
2025-09-10 15:33:07 +06:00
parent 458bdd3ee4
commit 4117288b21
2 changed files with 21 additions and 1 deletions

View File

@@ -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