-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2ffd3bd
commit 18b1130
Showing
4 changed files
with
22 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters