Add Result utils

This commit is contained in:
2023-07-31 09:51:18 +03:00
parent d24ab347cb
commit 03b4fc9f00

View File

@@ -13,3 +13,17 @@ inline fun <T, R> T.runCatchingCancellable(block: T.() -> R): Result<R> {
Result.failure(e)
}
}
inline fun <R, T : R> Result<T>.recoverCatchingCancellable(transform: (exception: Throwable) -> R): Result<R> {
return when (val exception = exceptionOrNull()) {
null -> this
else -> runCatchingCancellable { transform(exception) }
}
}
inline fun <R : Any, T : R> Result<T>.recoverNotNull(transform: (exception: Throwable) -> R?): Result<R> {
return when (val exception = exceptionOrNull()) {
null -> this
else -> transform(exception)?.let(Result.Companion::success) ?: this
}
}