Hi, what is the replacement for: ```IO { //somet...
# arrow
d
Hi, what is the replacement for:
Copy code
IO {
  //something may throw exception here
}.handleError { t -> 
  //map exception to some result
}
p
Hey @dnowak, one option would be to use
Copy code
either.runCatching {
    // do something
}.mapLeft {
    // Handle thrown exception
}
The
mapLeft
part could also be done with
Copy code
.fold(
{ // handle exception 
},
{ // handle success 
}
)
d
We use
Either<E, A
almost every where so in the old
IO
times when I had
IO<Either<E, A>>
then in
handleError
I only had to transform Exception into some left value.
Either.catch
in that case will return
Either<Throwable, Either<E, A>>
so
mapLeft
will leave me with
Either<E, Either<E, A>>
OK, I see it now I can write my own handleError for such nested Eithers
fold({mapError(it)}, ::identity}
p
depending on how it's used, you could also use the
mapLeft
-version with
bind()
, so
Copy code
either<e,a> {
  val successResult = either.runCatching {
    // do something
  }.mapLeft { mapError(it) }.bind()
}
where
mapError
has type
(Throwable)->E
d
Thanks a lot 🙂
👍 1