From b2e6ab51cbfd715bcb3775293ffa6384de29e157 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 15 Feb 2026 18:35:43 +0600 Subject: [PATCH] update runCatchingLogging to rethrow CanellationException --- .../coroutines/RunCatchingLogging.kt | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/RunCatchingLogging.kt b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/RunCatchingLogging.kt index 7dc9239cd03..a6d875ba1e8 100644 --- a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/RunCatchingLogging.kt +++ b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/RunCatchingLogging.kt @@ -2,11 +2,27 @@ package dev.inmo.micro_utils.coroutines import dev.inmo.kslog.common.KSLog import dev.inmo.kslog.common.e +import kotlin.coroutines.cancellation.CancellationException +/** + * Executes the given [block] within a `runCatching` context and logs any exceptions that occur, excluding + * `CancellationException` which is rethrown. This method simplifies error handling by automatically logging + * the errors using the provided [logger]. + * + * @param T The result type of the [block]. + * @param R The receiver type on which this function operates. + * @param errorMessageBuilder A lambda to build the error log message. By default, it returns a generic error message. + * @param logger The logging instance used for logging errors. Defaults to [KSLog]. + * @param block The code block to execute within the `runCatching` context. + * @return A [Result] representing the outcome of executing the [block]. + */ inline fun R.runCatchingLogging( noinline errorMessageBuilder: R.(Throwable) -> Any = { "Something web wrong" }, logger: KSLog = KSLog, block: R.() -> T ) = runCatching(block).onFailure { - logger.e(it) { errorMessageBuilder(it) } + when (it) { + is CancellationException -> throw it + else -> logger.e(it) { errorMessageBuilder(it) } + } }