Also trying to convert my resource release method ...
# arrow
s
Also trying to convert my resource release method from
IO<A>
to suspend ->
Either<Throwable, A>
, is this correct :
Copy code
fun Ds.closeTxn(exitCase: ExitCase<Throwable>): IO<Unit> {
    return IO {
        when (exitCase) {
            is ExitCase.Cancelled -> this.rollback().close()
            is ExitCase.Completed -> this.commit().close()
            is ExitCase.Error -> this.rollback().close()
        }
    }.guarantee(IO { this.close() })
}
to
Copy code
suspend fun Ds.closeTxn(e: arrow.fx.coroutines.ExitCase): Either<Throwable, Unit> {
    return guarantee(
        {
            Either.catch {
                when (e) {
                    is arrow.fx.coroutines.ExitCase.Completed -> this.commit().close()
                    is arrow.fx.coroutines.ExitCase.Cancelled -> this.rollback().close()
                    is arrow.fx.coroutines.ExitCase.Failure -> this.rollback().close()
                }
            }
        },
        { Either.catch { this.close() } }
    )
}
r
Yes Either.catch is the way to go to catch error on effects though not sure you need to catch closing the resource
We are simplifying so either is all you need for errors since being in suspend is also being inside IO already
suspend always implies the potential Throwable the continuation interface has to handle by it's resumeWith contract