diff options
Diffstat (limited to 'coroutine-utils/src')
-rw-r--r-- | coroutine-utils/src/main/kotlin/dev/msfjarvis/aps/util/coroutines/RunSuspendCatching.kt | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/coroutine-utils/src/main/kotlin/dev/msfjarvis/aps/util/coroutines/RunSuspendCatching.kt b/coroutine-utils/src/main/kotlin/dev/msfjarvis/aps/util/coroutines/RunSuspendCatching.kt new file mode 100644 index 00000000..7e5b906c --- /dev/null +++ b/coroutine-utils/src/main/kotlin/dev/msfjarvis/aps/util/coroutines/RunSuspendCatching.kt @@ -0,0 +1,48 @@ +@file:OptIn(ExperimentalContracts::class) +@file:Suppress("RedundantSuspendModifier") + +package dev.msfjarvis.aps.util.coroutines + +import com.github.michaelbull.result.Err +import com.github.michaelbull.result.Ok +import com.github.michaelbull.result.Result +import kotlin.contracts.ExperimentalContracts +import kotlin.contracts.InvocationKind +import kotlin.contracts.contract +import kotlinx.coroutines.CancellationException + +/** + * Calls the specified function [block] with [this] value as its receiver and returns its + * encapsulated result if invocation was successful, catching any [Throwable] except + * [CancellationException] that was thrown from the [block] function execution and encapsulating it + * as a failure. + */ +public suspend inline fun <V> runSuspendCatching(block: () -> V): Result<V, Throwable> { + contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } + + return try { + Ok(block()) + } catch (e: Throwable) { + if (e is CancellationException) throw e + Err(e) + } +} + +/** + * Calls the specified function [block] with [this] value as its receiver and returns its + * encapsulated result if invocation was successful, catching any [Throwable] except + * [CancellationException] that was thrown from the [block] function execution and encapsulating it + * as a failure. + */ +public suspend inline infix fun <T, V> T.runSuspendCatching( + block: T.() -> V +): Result<V, Throwable> { + contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } + + return try { + Ok(block()) + } catch (e: Throwable) { + if (e is CancellationException) throw e + Err(e) + } +} |