我想在我的play scala web應用程序中進行錯誤處理。錯誤處理Scala:未來理解
我的應用程序與數據庫進行通信以獲取一些行,它遵循以下流程。
- 首先調用數據庫獲取一些數據
- 使用在第一次調用的數據從數據庫
- 窗體使用來自最後兩個調用數據庫接收到的數據的響應獲取其他數據。
下面是我的僞代碼。
def getResponse(name: String)
(implicit ctxt: ExecutionContext): Future[Response] = {
for {
future1 <- callFuture1(name)
future2 <- callFuture2(future1.data)
future3 <- callFuture3(future1.data, future2.data)
} yield future3
}
上述理解中的每個方法都返回一個未來,這些方法的簽名如下。
private def callFuture1(name: String)
(implicit ctxt: ExecutionContext): Future[SomeType1] {...}
private def callFuture2(keywords: List[String])
(implicit ctxt: ExecutionContext): Future[SomeType2] {...}
private def callFuture3(data: List[SomeType3], counts: List[Int])
(implicit ctxt: ExecutionContext): Future[Response] {...}
怎樣對待錯誤/故障處理,在下列情況下
- 當callFuture1無法從數據庫中提取數據。我想返回 錯誤消息的相應錯誤響應。由於callFuture2 僅在callFuture1之後執行。我不想執行 callFuture2,如果callFuture1失敗/錯誤並且想立即返回 錯誤消息。 (對於callFuture2和 callFuture3同樣的事情)
- 編輯 -
我試圖返回從GETRESPONSE()方法,當任一callFuture的失敗,而不是繼續執行後續的相應的錯誤信息futureCalls。
我嘗試以下,根據彼得Neyens答案,但給了我一個運行時錯誤..
def getResponse(name: String)
(implicit ctxt: ExecutionContext): Future[Response] = {
for {
future1 <- callFuture1(name) recoverWith {
case e:Exception => return Future{Response(Nil,Nil,e.getMessage)}
}
future2 <- callFuture2(future1.data)
future3 <- callFuture3(future1.data, future2.data)
} yield future3
}
運行時錯誤,我得到
ERROR] [08/31/2015 02:09:45.011] [play-akka.actor.default-dispatcher-3] [ActorSystem(play)] Uncaught error from thread [play-akka.actor.default-dispatcher-3] (scala.runtime.NonLocalReturnControl)
[error] a.a.ActorSystemImpl - Uncaught error from thread [play-akka.actor.default-dispatcher-3]
scala.runtime.NonLocalReturnControl: null
感謝您的回答。這很有幫助。我仍然不確定這是否解決了我的問題。 1)使用方法1,(不使用隱式類),當我恢復Future.failed(新的異常()),引發異常,我的執行不會繼續。我試圖在任何futurecall失敗時返回一個適當的ResponseMessage,而不繼續後續futurecalls(我將更新問題以使其更清楚) – konquestor
Future.fail(new Exception(...))導致執行拋出一個例外。相反,我想返回一個適當的錯誤消息,當futurecall失敗/錯誤。 – konquestor
嘗試像我的答案中的最後一個代碼示例。 –