Skip to content

Instantly share code, notes, and snippets.

@vasigorc
Created December 1, 2022 19:44
Show Gist options
  • Save vasigorc/2881e27587de3c246740fa2a38632075 to your computer and use it in GitHub Desktop.
Save vasigorc/2881e27587de3c246740fa2a38632075 to your computer and use it in GitHub Desktop.
Confirm that an exception is not lost with EitherT over Future when doing flatMapF and valueOrF
package ca.vgorcinschi.cats
import cats.data.EitherT
import cats.implicits._
import java.io.IOException
import scala.concurrent.Future
import scala.util.{Failure, Success}
object EitherTFutureTest extends App {
import concurrent.ExecutionContext.Implicits.global
val externalFailedCall: Future[Unit] = Future.failed(new IOException("boom"))
val externalSuccessfulCall: Future[Unit] = Future.unit
def methodUnderTest: Future[Int] = {
val either: Either[Exception, Int] = 1.asRight
EitherT
.fromEither[Future](either)
.flatMap(_ => EitherT.right[Throwable](externalFailedCall))
.flatMapF(_ => Future.successful(either))
.valueOrF(exc => Future.failed(exc))
}
methodUnderTest onComplete {
case Success(value) =>
println(s"Successfully printed $value!")
case Failure(exception) => System.err.println(s"Oops: ${exception.getStackTrace.mkString("\n")}")
}
Thread.sleep(2000)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment