catch
Catch allows for transforming Throwable in the Either.Left side. This API is the same as recover, but offers the same APIs for working over Throwable as Effect&EagerEffect.
This is useful when working with Either.catch since this API offers a reified variant. The reified version allows you to refine Throwable to T : Throwable, where any Throwable not matching the t is T predicate will be rethrown.
import arrow.core.Either
import arrow.core.catch
import io.kotest.assertions.throwables.shouldThrowUnit
import io.kotest.matchers.shouldBe
fun test() {
val left: Either<Throwable, Int> = Either.catch { throw RuntimeException("Boom!") }
val caught: Either<Nothing, Int> = left.catch { _: Throwable -> 1 }
val failure: Either<String, Int> = left.catch { _: Throwable -> shift("failure") }
shouldThrowUnit<RuntimeException> {
val caught2: Either<Nothing, Int> = left.catch { _: IllegalStateException -> 1 }
}
caught shouldBe Either.Right(1)
failure shouldBe Either.Left("failure")
}Content copied to clipboard