I'm using http4s, and I have a Try
that generates some json data for a response:
case GET -> Root / "something" =>
getSomethingTry() match {
case Success(something) => Ok(something)
case Failure(CustomNotFoundException(reason)) => NotFound(reason)
case Failure(CustomConflictException()) => Conflict()
}
This function correctly returns a Task[Response]
However, I want to replace the Try
with a Future
. Matching no longer works, because the future may not have been resolved at the time of the match. So, I can map the future:
case GET -> Root / "something" =>
getSomethingFuture().map {
something => Ok(something)
}.recover {
case CustomNotFoundException(reason) => NotFound(reason)
case CustomConflictException() => Conflict()
}
But this returns a Future[Task[Response]]
which is not what http4s wants. It doesn't seem appropriate to use Await.result
to unbox the Future
- I think this could cause thread pool issues - but it does make the code work.
http4s accepts futures as the argument to the task creator:
case GET -> Root / "something" =>
Ok(getSomethingFuture())
But this doesn't let me set different status codes in the event of different errors. A solution could be to do a .recover
on a task, but I can't see an obvious way to do that.
How can I call different http4s task wrappers in the event of different Future
failure cases? Do I need to use middleware?