In Play Framework 2.3, an action can produce a result from a successful future call like this:
def index = Action.async {
val futureInt = scala.concurrent.Future { intensiveComputation() }
futureInt.map(i => Ok("Got result: " + i))
}
But how can an action deal with a failed future call, i.e., a future that was completed by calling failure()
instead of success()
?
For instance, how could an action produce a InternalServerError
result with the message returned in the future's failure's throwable?
onComplete
and onFailure
callbacks don't seem to fit the action's flow (it needs to return a result, either from a successful future or a failed one).
For a single
Action
, you can do this withrecover
, recovering the failedFuture
to aResult
:recover
in this case is aPartialFunction[Throwable, Result]
, so you can be more fine grained with your error handling, and anything that is not defined in thePartialFunction
will remain a failedFuture
. More generally, you could probably use a custom definedAction
that implements this.