catch
infix fun <E, A> Effect<E, A>.catch(resolve: suspend Raise<E>.(throwable: Throwable) -> A): Effect<E, A>
Catch any unexpected exceptions, and resolve them. You can either return a value a new value of A, or short-circuit the effect by raising with a value of E, or raise an exception into suspend.
import arrow.core.raise.effect
import arrow.core.raise.catch
object User
object Error
val exception = effect<Error, User> { throw RuntimeException("BOOM") } // Exception(BOOM)
val a = exception.catch { error -> error.message?.length ?: -1 } // Success(5)
val b = exception.catch { raise(Error) } // Raise(error)
val c = exception.catch { throw RuntimeException("other-failure") } // Exception(other-failure)Content copied to clipboard
infix inline fun <T : Throwable, E, A> Effect<E, A>.catch(crossinline recover: suspend Raise<E>.(T) -> A): Effect<E, A>
A version of catch that refines the Throwable to T. This is useful for wrapping foreign code, such as database, network calls, etc.
import arrow.core.raise.effect
import arrow.core.raise.catch
object User
object Error
val x = effect<Error, User> {
throw IllegalArgumentException("builder missed args")
}.catch { raise(Error) }Content copied to clipboard
If you don't need an error value when wrapping your foreign code you can use Nothing to fill the type parameter.
val y = effect<Nothing, User> {
throw IllegalArgumentException("builder missed args")
}.catch<IllegalArgumentException, Error, User> { raise(Error) }Content copied to clipboard
Runs the Effect and captures any nonFatalOrThrow exception into Result.
infix fun <E, A> EagerEffect<E, A>.catch(recover: Raise<E>.(throwable: Throwable) -> A): EagerEffect<E, A>