When using http4s, I came across a scenario where I need to convert an Either to an IO.
case GET -> root / start / end / "overall" =>
for {
s <- FooAdt.fromString(start)
e <- FooAdt.fromString(end)
things <- thingsRepo.get(s, e)
} yield
Ok(things.asJson)
The above code will not compiler because here FooAdt.fromString returns an Either[Error, MyAdt] and thingsRepo.get returns an IO and that is also what the return container of the route is expected to be (IO).
What would be the idiomatic way of approaching the problem here?
If it isn’t, then the easiest thing to do is probably converting Error to a Throwable.
e.g.
case class ThrowableError(error: Error) extends Exception
FooAdt.fromString(start).fold(ThrowableError andThen IO.raiseError, IO.pure)
// or
import cats.implicits._
FooAdt.fromString(start).leftMap(ThrowableError).liftTo[IO]