Skip to content

Commit

Permalink
Adds Result#handleFailureWith to easily recover from failures (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
cwmyers authored Jul 31, 2024
1 parent 6dc1cc9 commit 030c57f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/api/lib.api
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ public final class app/cash/quiver/extensions/ResultKt {
public static final fun failure (Ljava/lang/Throwable;)Ljava/lang/Object;
public static final fun flatTap (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
public static final fun flatten (Ljava/lang/Object;)Ljava/lang/Object;
public static final fun handleFailureWith (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
public static final fun isFailure (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Z
public static final fun isSuccess (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Z
public static final fun mapFailure (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
Expand Down
7 changes: 7 additions & 0 deletions lib/src/main/kotlin/app/cash/quiver/extensions/Result.kt
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,10 @@ inline fun <T> Result<T>.isFailure(predicate: (Throwable) -> Boolean): Boolean =
*/
inline fun <T> Result<T>.isSuccess(predicate: (T) -> Boolean): Boolean =
fold(onFailure = { false }, onSuccess = predicate)

/**
* Recovers errors with a function from Throwable to Result.
*/
inline fun <T> Result<T>.handleFailureWith(f: (Throwable) -> Result<T>): Result<T> = recoverCatching {
f(it).getOrThrow()
}
13 changes: 12 additions & 1 deletion lib/src/test/kotlin/app/cash/quiver/extensions/ResultTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import app.cash.quiver.matchers.shouldBePresent
import arrow.core.None
import arrow.core.Some
import arrow.core.raise.result
import arrow.core.toOption
import io.kotest.assertions.arrow.core.shouldBeLeft
import io.kotest.assertions.arrow.core.shouldBeRight
import io.kotest.assertions.throwables.shouldThrow
Expand Down Expand Up @@ -127,4 +126,16 @@ class ResultTest : StringSpec({
Result.failure<String>(Exception("hello")).isSuccess { it == "hello" } shouldBe false
}

"handleFailureWith recovers errors" {
1.success().handleFailureWith { Result.failure(Throwable("never happens")) }.shouldBeSuccess(1)
Throwable("sad panda").failure<Int>().handleFailureWith { Result.failure(Throwable("even sadder")) }
.shouldBeFailure().message shouldBe "even sadder"

Throwable("sad panda").failure<Int>().handleFailureWith { 1.success() }
.shouldBeSuccess(1)

Throwable("sad panda").failure<Int>().handleFailureWith { it.failure() }
.shouldBeFailure().message shouldBe "sad panda"
}

})

0 comments on commit 030c57f

Please sign in to comment.