what would be the way to log the exception (just l...
# arrow
s
what would be the way to log the exception (just log, do nothing to recover the failure) in case an
IO
fails?
t
Use
flatTap
. It perform an effect without changing the result.
Copy code
IO(1).map { it + 2 } 
    .flatTap { 
        IO.effect { log.debug(it) } 
    }
The result of this code is 3 and in console you can see 3
👍 1
s
that would also log success 😄
t
oh
s
let me try again, this is what I came up with:
Copy code
fun <A> IO<A>.logError(log: (Throwable, () -> String) -> IO<Unit>, msg: () -> String): IO<A> =
    handleErrorWith { log(it, msg).followedBy(IO.raiseError(it)) }
☝️ 1
👍 1
where
log
is the function that logs
any thoughts on it
t
I think this is quite good solution. But I came up with
attempt
and folding either... Not so clean as your
arrow 1
p
handleErrorWith { IO { log(it, msg }.followedBy(IO.raiseError(it)) }
yep
😄
s
EitherT<IO, A, B>
this will help.
s
I'm trying to stay away from MTL 😁
s
Oh! Just curious to know, is there any downside of using that?
s
p
It’ll be miles better after the compiler plugins refactor happens
☝️ 1
@raulraja has all the magic necessary to make them performant and fast
j
Also EitherT on the left side, just as bio will only contain the explicit error but io may contain more. Usually that's enough but for logging you might want both