Skip to content

Commit

Permalink
address more PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
JesusMcCloud committed Oct 16, 2024
1 parent 2ffd3bd commit 18b1130
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 23 deletions.
10 changes: 6 additions & 4 deletions kmmresult/src/commonMain/kotlin/at/asitplus/KmmResult.kt
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,9 @@ inline fun <R, T : R> KmmResult<T>.recoverCatching(block: (error: Throwable) ->

/**
* Non-fatal-only-catching version of stdlib's [runCatching], directly returning a [KmmResult] --
* Re-throws any fatal exceptions, such as `OutOfMemoryError`. Relies on [Arrow](https://arrow-kt.io)'s
* [nonFatalOrThrow](https://apidocs.arrow-kt.io/arrow-core/arrow.core/non-fatal-or-throw.html) logic.
* Re-throws any fatal exceptions, such as `OutOfMemoryError`. Re-implements [Arrow](https://arrow-kt.io)'s
* [nonFatalOrThrow](https://apidocs.arrow-kt.io/arrow-core/arrow.core/non-fatal-or-throw.html)
* logic to avoid a dependency on Arrow for a single function.
*/
@Suppress("TooGenericExceptionCaught")
inline fun <T> catching(block: () -> T): KmmResult<T> {
Expand All @@ -286,8 +287,9 @@ inline fun <T> catching(block: () -> T): KmmResult<T> {
/**
* Non-fatal-only-catching version of stdlib's [runCatching] (calling the specified function [block] with `this` value
* as its receiver), directly returning a [KmmResult] --
* Re-throws any fatal exceptions, such as `OutOfMemoryError`. Relies on [Arrow](https://arrow-kt.io)'s
* [nonFatalOrThrow](https://apidocs.arrow-kt.io/arrow-core/arrow.core/non-fatal-or-throw.html) logic.
* Re-throws any fatal exceptions, such as `OutOfMemoryError`. Re-implements [Arrow](https://arrow-kt.io)'s
* [nonFatalOrThrow](https://apidocs.arrow-kt.io/arrow-core/arrow.core/non-fatal-or-throw.html)
* logic to avoid a dependency on Arrow for a single function.
*/
@Suppress("TooGenericExceptionCaught")
inline fun <T, R> T.catching(block: T.() -> R): KmmResult<R> {
Expand Down
33 changes: 14 additions & 19 deletions kmmresult/src/commonMain/kotlin/at/asitplus/NonFatal.kt
Original file line number Diff line number Diff line change
@@ -1,42 +1,37 @@
package at.asitplus

import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

/**
* Throws any fatal exceptions. This is a re-implementation taken from Arrow's
* [`nonFatalOrThrow`](https://apidocs.arrow-kt.io/arrow-core/arrow.core/non-fatal-or-throw.html) –
* not because it is bad, it is actually pretty much perfect.
* However, the arrow dependency triggered an obscure IDEA bug, resulting in `NoClasDefFoundErrors` instead of correct
* behaviour. * We therefore removed the dependency and added the functionality directly to KmmResult.
*
* Please note that this was never a problem building anything that depended on KmmResult, it only made debugging
* in IDEA a nightmare.
* to avoid a dependency on Arrow for a single function.
*/
@Suppress("NOTHING_TO_INLINE")
expect inline fun Throwable.nonFatalOrThrow(): Throwable

/**
* Helper to effectively convert stdlib's [runCatching] to behave like KmmResults Non-fatal-only [catching]. I.e. any
* Helper to effectively convert stdlib's [runCatching] to behave like KmmResult's Non-fatal-only [catching]. I.e. any
* fatal exceptions are thrown.
* The reason this exists is that [catching] incurs instantiation cost.
* This helper hence provides the best of both worlds.
*/
@Suppress("NOTHING_TO_INLINE")
inline fun <T> Result<T>.nonFatalOrThrow(): Result<T> = this.onFailure { it.nonFatalOrThrow() }


/**
* Non-fatal-only-catching version of stdlib's [runCatching], returning a [Result] --
* Re-throws any fatal exceptions, such as `OutOfMemoryError`. Relies on [Arrow](https://arrow-kt.io)'s
* [nonFatalOrThrow](https://apidocs.arrow-kt.io/arrow-core/arrow.core/non-fatal-or-throw.html) logic.
* Re-throws any fatal exceptions, such as `OutOfMemoryError`. Re-implements [Arrow](https://arrow-kt.io)'s
* [nonFatalOrThrow](https://apidocs.arrow-kt.io/arrow-core/arrow.core/non-fatal-or-throw.html)
* logic to avoid a dependency on Arrow for a single function.
*/
@Suppress("TooGenericExceptionCaught")
inline fun <T> catchingUnwrapped(block: () -> T): Result<T> = runCatching (block).nonFatalOrThrow()
@Suppress("NOTHING_TO_INLINE")
inline fun <T> catchingUnwrapped(block: () -> T): Result<T> = runCatching(block).nonFatalOrThrow()

/**
* Non-fatal-only-catching version of stdlib's [runCatching] (calling the specified function [block] with `this` value
* as its receiver), directly returning a [Result] --
* Re-throws any fatal exceptions, such as `OutOfMemoryError`. Relies on [Arrow](https://arrow-kt.io)'s
* [nonFatalOrThrow](https://apidocs.arrow-kt.io/arrow-core/arrow.core/non-fatal-or-throw.html) logic.
* Re-throws any fatal exceptions, such as `OutOfMemoryError`. Re-implements [Arrow](https://arrow-kt.io)'s
* [nonFatalOrThrow](https://apidocs.arrow-kt.io/arrow-core/arrow.core/non-fatal-or-throw.html)
* logic to avoid a dependency on Arrow for a single function.
*/
@Suppress("TooGenericExceptionCaught")
inline fun <T, R> T.catchingUnwrapped(block: T.() -> R): Result<R> = runCatching (block).nonFatalOrThrow()
@Suppress("NOTHING_TO_INLINE")
inline fun <T, R> T.catchingUnwrapped(block: T.() -> R): Result<R> = runCatching(block).nonFatalOrThrow()
1 change: 1 addition & 0 deletions kmmresult/src/jvmMain/kotlin/at/asitplus/NonFatal.jvm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package at.asitplus

import kotlin.coroutines.cancellation.CancellationException
//Taken from Arrow: https://github.com/arrow-kt/arrow/blob/99de6148320a4299a5aef20686a6063ca732026b/arrow-libs/core/arrow-core/src/jvmMain/kotlin/arrow/core/NonFatal.kt
@Suppress("NOTHING_TO_INLINE")
actual inline fun Throwable.nonFatalOrThrow(): Throwable = when (this) {
is VirtualMachineError, is ThreadDeath, is InterruptedException, is LinkageError, is CancellationException -> throw this
else -> this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package at.asitplus

import kotlin.coroutines.cancellation.CancellationException
//Taken from Arrow: https://github.com/arrow-kt/arrow/blob/99de6148320a4299a5aef20686a6063ca732026b/arrow-libs/core/arrow-core/src/nonJvmMain/kotlin/arrow/core/NonFatal.kt
@Suppress("NOTHING_TO_INLINE")
actual inline fun Throwable.nonFatalOrThrow(): Throwable = when (this) {
is CancellationException -> throw this
else -> this
Expand Down

0 comments on commit 18b1130

Please sign in to comment.