假设我在Play应用程序中使用的某些服务中定义了以下方法,每个方法都会在失败时执行一些命令,更新数据库并异步发送一些通知。
def thisMightFail(): Future[SomeResult]
def thisUpdatesStatus(): Future[Unit]
def thisSendsNotification(): Future[Unit]
thisUpdatesStatus
和thisSendsNotification
彼此独立,并被要求进行如下所示的错误处理。 (如果失败,则无法进一步执行)
for {
_ <- Future{ Some process }
result <- thisMightFail() transform {
case Success(v) => v
case Failure(cause) =>
val f = Future.sequence(List(
thisUpdatesStatus(),
thisSendsNotification()
))
Failure(new Exception("Command execution failed", cause))
}
_ <- Future{ Another process that uses "result" }
:
} yield ...
我的问题是在返回f
之前应该等待Failure(cause)
还是在这种情况下有更好的方法来处理错误?