恢复未来没有按预期响应(scala)
recover future is not responding as expected (scala)
我有一个自定义案例 class 异常:
case class RecordNotFoundException(msg: String) extends RuntimeException(msg)
在我的 dao 中,我有一个从数据库中提取对象的方法,这是 return 的未来,如果这个未来失败,我将抛出我的 RecordNotFoundException 异常:
def getPerson(personId: String): Future[Person] = {
val q = quote {
query[Person].filter(per => per.personId == lift(personId))
}
ctx.run.map(_.head) recover {
case ex: NoSuchElementException => throw RecordNotFoundException(s"personId $personId does not exist")
}
}
在另一个方法中,我调用了 getPerson 方法,所以我将恢复添加到这个另一个方法中,我想 return 在未来因 RecordNotFoundException 失败时做一些事情:
def run(personDao: PersonDao): Future[String] = {
if (true) {
for {
person <- personDao.getPerson("some_user_id")
something <- someOtherFuture
} yield {
// what happens here not relevant
}
} else {
Future.successful("got to the else")
} recover {
case e: RecordNotFoundException => "got to the recover"
}
}
所以基本上我希望 运行() 方法在 getPerson 失败时 return "got to the recover",但由于某种原因我没有恢复...失败又回来了到控制器。
有谁知道这是为什么吗?
首先看看你的recover
在哪里。你为什么不把它移到方法的最后一行?类似于:
def getPerson(id: String): Future[String] = Future("Andy")
def someOtherFuture(id: String) = Future("Mandy")
case class RecordNotFoundException(msg: String) extends RuntimeException(msg)
def run(personDao: String): Future[String] = {
if (true)
for {
person <- getPerson("some_user_id")
something <- someOtherFuture("1")
} yield {
person
}
else Future.successful("got to the else")
}recover { case e: RecordNotFoundException => "got to the recover"}
也为 getPerson
移动 recover
。
在我看来,在您的模型 and/or 服务中使用 Future/recover
并没有错,然后异常会返回到控制器。然后控制器方法处理自定义的异常;和 return InternalServerError
或 BadRequest
给用户或 API 电话。
我有一个自定义案例 class 异常:
case class RecordNotFoundException(msg: String) extends RuntimeException(msg)
在我的 dao 中,我有一个从数据库中提取对象的方法,这是 return 的未来,如果这个未来失败,我将抛出我的 RecordNotFoundException 异常:
def getPerson(personId: String): Future[Person] = {
val q = quote {
query[Person].filter(per => per.personId == lift(personId))
}
ctx.run.map(_.head) recover {
case ex: NoSuchElementException => throw RecordNotFoundException(s"personId $personId does not exist")
}
}
在另一个方法中,我调用了 getPerson 方法,所以我将恢复添加到这个另一个方法中,我想 return 在未来因 RecordNotFoundException 失败时做一些事情:
def run(personDao: PersonDao): Future[String] = {
if (true) {
for {
person <- personDao.getPerson("some_user_id")
something <- someOtherFuture
} yield {
// what happens here not relevant
}
} else {
Future.successful("got to the else")
} recover {
case e: RecordNotFoundException => "got to the recover"
}
}
所以基本上我希望 运行() 方法在 getPerson 失败时 return "got to the recover",但由于某种原因我没有恢复...失败又回来了到控制器。
有谁知道这是为什么吗?
首先看看你的recover
在哪里。你为什么不把它移到方法的最后一行?类似于:
def getPerson(id: String): Future[String] = Future("Andy")
def someOtherFuture(id: String) = Future("Mandy")
case class RecordNotFoundException(msg: String) extends RuntimeException(msg)
def run(personDao: String): Future[String] = {
if (true)
for {
person <- getPerson("some_user_id")
something <- someOtherFuture("1")
} yield {
person
}
else Future.successful("got to the else")
}recover { case e: RecordNotFoundException => "got to the recover"}
也为 getPerson
移动 recover
。
在我看来,在您的模型 and/or 服务中使用 Future/recover
并没有错,然后异常会返回到控制器。然后控制器方法处理自定义的异常;和 return InternalServerError
或 BadRequest
给用户或 API 电话。