演員(OrgaActor)如何請求文檔的數據庫演員(DbActor)。 DbActor並不總是提供文檔,但也是一個數據庫錯誤異常。 OrgaActor現在應該使用詳細信息(它實際上是組織查詢,而不是通用數據庫查詢)來更改接收到的異常。我努力攔截來自DbActor的異常,並在發送給控制器的結果中更改它(OrgaCtl)。的OrgaActor在遊戲框架,以攔截和更改例外斯卡拉阿卡2.4.6
def receive = {
case GetDocument(id: String) => {
try {
val answer = (dbActor ? DbActor.GetDocument("Orga", id)).mapTo[JsValue]
play.Logger.debug("in answer")
answer.pipeTo(sender())
} catch {
case e: Exception =>
play.Logger.debug("in exception")
val fhe = error.FhException(e, 404, error.Error.ORGA_LOAD, "error on load " + id)
sender() ! akka.actor.Status.Failure(fhe)
}
}
非工作示例代碼DbActor的GetDocument:
case GetDocument(coll: String, id: String) =>
try {
val response = new FigureheadDb().get(coll, id)
sender() ! response.jsonElementPayload()
} catch {
case e: Exception =>
val fhe = error.FhException(e, 404, error.Error.FH_ID, "error on load " + id + " in coll " + coll)
sender() ! akka.actor.Status.Failure(fhe)
throw fhe
}
從未顯示的inException調試消息,所以我想扔FHE代碼DbActor永遠不會到達呼叫OrgaActor。 DbActor被綁定到OrgaActor與val dbActor = context.actorOf(Props [DbActor],name =「db-actor」)。
問題是:如何攔截組織參與者中的數據庫參與者拋出的錯誤,並將強化的錯誤傳遞給全局錯誤處理程序?目前,全局錯誤處理程序總是獲取數據庫參與者錯誤。
case GetDocument(coll: String, id: String, originalSender: ActorRef) =>
try {
val response = new FigureheadDb().get(coll, id)
sender() ! GetDocumentSuccess(response.jsonElementPayload(), originalSender)
} catch {
case e: Exception =>
val fhe = error.FhException(e, 404, error.Error.FH_ID, "error on load " + id + " in coll " + coll)
sender() ! GetDocumentFailed(fhe, id, originalSender)
}
OrgaActor:
case GetDocument(id: String) => {
val answer : Future[Any] = dbActor ? DbActor.GetDocument(coll, id, sender())
answer.map {
case success: DbActor.GetDocumentSuccess =>
play.Logger.debug("in success")
success.originalSender ! success.result.as[JsValue]
case failure: DbActor.GetDocumentFailed => {
play.Logger.debug("in failure")
val fhe = error.FhException(failure.cause, 404, error.Error.ORGA_LOAD, "error on load " + failure.id)
failure.originalSender ! akka.actor.Status.Failure(fhe)
}
case any => {
play.Logger.error("Dead end")
}
}
}
對象DbActor:基於公認的答案
dbActor
碼
case class GetDocument(coll: String, id: String, originalSender: ActorRef)
// http://stackoverflow.com/questions/35549414/how-to-intercept-and-change-exception-in-scala-akka-in-play-framework-2-4-6
sealed trait GetDocumentResult
case class GetDocumentSuccess(result: JsValue, originalSender: ActorRef) extends GetDocumentResult
case class GetDocumentFailed(cause: Exception, id: String, originalSender: ActorRef) extends GetDocumentResult