arrow-fx / arrow.fx / arrow.Kind / handleErrorWith

handleErrorWith

fun <A> IOOf<A>.handleErrorWith(f: (Throwable) -> IOOf<A>): IO<A>

Handle the error by resolving the error with an effect that results in A.

import arrow.fx.IO
import arrow.fx.handleErrorWith
import arrow.fx.typeclasses.milliseconds

fun main(args: Array<String>) {
  fun getMessage(e: Throwable): IO<String> = IO.sleep(250.milliseconds)
    .followedBy(IO.effect { "Delayed goodbye World! after $e" })

  //sampleStart
  val result = IO.raiseError<Int>(RuntimeException("Boom"))
    .handleErrorWith { e -> getMessage(e) }
  //sampleEnd
  println(result.unsafeRunSync())
}

See Also

handleErrorWith

Do you like Arrow?

Arrow Org
<