我想要一个 Action.async (1)尝试从数据库中获取值。如果数据库不可用,它将尝试连接到另一个资源并 (2) 从那里获取值。因为我使用的两个资源返回 Future,所以我用“recover”关键字将它们分开。我不确定这是否是最好的方法......但是 recovery{} 里面的语句有一个类型不匹配错误:
def show(url: String) = Action.async { implicit request: Request[AnyContent] =>
println("url: " + url)
val repositoryUrl = RepositoryUrl(url)
val repositoryId = RepositoryId.createFromUrl(url)
// Listing commits from the DB
val f: Future[Seq[Commit]] = commit.listByRepository(repositoryId.toString())
f.map { f: Seq[Commit] =>
val json = JsObject(Seq(
"project URL" -> JsString(url),
"list of commits" -> Json.toJson(f)))
Ok(json)
}.recover {
case e: scala.concurrent.TimeoutException =>
// InternalServerError("timeout")
// Listing commits from the Git CLI
val github = rules.GitHub(repositoryUrl)
val seq: Future[Seq[Commit]] = github.listCommits
seq.map { seq: Seq[Commit] =>
val json = JsObject(Seq(
"project URL" -> JsString(url),
"list of commits" -> Json.toJson(seq)))
Ok(json)
}
}
}
我得到了type mismatch; found : scala.concurrent.Future[play.api.mvc.Result] required: play.api.mvc.Result
在线错误seq.map { seq: Seq[Commit] =>
。如果我的未来失败了,我该如何返回另一个结果?
谢谢!