【发布时间】:2018-06-15 19:24:07
【问题描述】:
我对 Scala 有一个理解,我想处理 Await.result 的异常。问题是我的捕获显示None.get,但不是我定义的异常。
异常在case e: Exception => BadRequest(s"Exception found: ${e.getMessage}") 被捕获,但它是一个Future。我想显示 getMessage。
try {
val futureResult = for {
futureRackRow <- rackRepository.getById(rack.id) recoverWith {
case e: Exception => throw RackException(s"Error on select Rack: ${e.getMessage}")
}
futureSeqGpuRow <- gpuRepository.getByRack(futureRackRow.get.id) recoverWith {
case e: Exception => throw GpuException(s"Error on select Gpu's from Rack: ${e.getMessage}")
}
} yield (futureRackRow, futureSeqGpuRow)
println(2)
val result = Await.result(futureResult, 20 seconds) // The exception comes here ....
result._1 match {
case Some(rackRow) => ???
case None => ???
}
Ok
} catch {
case re: RackException => BadRequest(s"Rack Exception: ${re.getMessage}")
case ge: GpuException => BadRequest(s"Gpu Exception: ${ge.getMessage}")
case e: Exception => BadRequest(s"Exception found: ${e.getMessage}")
}
【问题讨论】:
-
您发布的代码没有
Option.get,因此不清楚如何获得None.get异常