Dealing with failed futures

 ̄綄美尐妖づ 提交于 2019-12-04 09:24:30

For a single Action, you can do this with recover, recovering the failed Future to a Result:

def index = Action.async {
    val futureInt = scala.concurrent.Future { intensiveComputation() }
    futureInt.map(i => Ok("Got result: " + i))
        .recover{ case e: Exception => InternalServerError(e.getMessage) }
}

recover in this case is a PartialFunction[Throwable, Result], so you can be more fine grained with your error handling, and anything that is not defined in the PartialFunction will remain a failed Future. More generally, you could probably use a custom defined Action that implements this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!