This issue sounds somewhat familiar, and apologies if I forgot the exact discussion, however I stumbled on it in the wild in the context of typelevel/fs2#2895 and found it pretty confusing.
In particular:
- the behaviour of
MonadThrow and MonadCancelThrow is inconsistent
- the behaviour of
MonadCancelThrow is weird: excluding cancelation, if something short circuits a flatMap I expect to be able to handleError it. Ofc this expectation is broken when you have two error channels, but MonadThrow works around it
val a: EitherT[IO, Throwable, Unit] =
EitherT.leftT[IO, Unit](new Exception("Yooo"))
val console = Console[EitherT[IO, Throwable, *]]
def foo(fa: EitherT[IO, Throwable, Unit]): EitherT[IO, Throwable, Unit] =
fa.flatMap(_ => console.println("not printed"))
.handleError(_ => ())
def bar[F[_]: MonadThrow: Console](fa: F[Unit]): F[Unit] =
fa.flatMap(_ => Console[F].println("not printed"))
.handleError(_ => ())
def baz[F[_]: MonadCancelThrow: Console](fa: F[Unit]): F[Unit] =
fa.flatMap(_ => Console[F].println("not printed"))
.handleError(_ => ())
def x = foo(a).value.unsafeRunSync()
def y = bar(a).value.unsafeRunSync()
def z = baz(a).value.unsafeRunSync()
scala> x
val res0: Either[Throwable,Unit] = Right(())
scala> y
val res1: Either[Throwable,Unit] = Right(())
scala> z
val res2: Either[Throwable,Unit] = Left(java.lang.Exception: Yooo)
This issue sounds somewhat familiar, and apologies if I forgot the exact discussion, however I stumbled on it in the wild in the context of typelevel/fs2#2895 and found it pretty confusing.
In particular:
MonadThrowandMonadCancelThrowis inconsistentMonadCancelThrowis weird: excluding cancelation, if something short circuits aflatMapI expect to be able tohandleErrorit. Ofc this expectation is broken when you have two error channels, but MonadThrow works around it