How to Catch Future Exception in Game Structure 2.4
I am trying to figure out how to catch a future exception in a function called by an asynchronous action in Play Framework 2.4. However, the code I have using restore seems to never get executed. I always get an execution exception page, not an "Ok" response.
Action code:
def index = Action.async {
cardRepo.getAll()
.map {
cards => Ok(views.html.cardlist(cards))
}.recover{
case e: Exception => Ok(e.getMessage)
}
}
The code in cardRepo.getAll (which I have programmed for a new Exception experiment to experiment with):
def getAll(): Future[Seq[Card]] = {
implicit val cardFormat = Json.format[Card]
val cards = collection.find(Json.obj())
.cursor[Card]()
.collect[Seq]()
throw new Exception("OH DEAR")
cards
}
I've seen similar questions on Stack Overflow, but I can't see what I'm doing wrong.
+3
source to share