当 Actor returns Future[Option[None]] Akka Http 时抛出异常
Throwing exception when Actor returns Future[Option[None]] Akka Http
我有案例class
case class Fare(flightNumber: String, flightDate: LocalDate, amount: Int)
和演员
class FareDb extends Actor with ActorLogging {
import FareDb._
var fares: List[Fare] = List()
override def receive: Receive = {
case AddFare(fare) =>
log.info(s"Adding fare $fare")
fares = fares :+ fare
case GetFare(flightNumber, flightDate) =>
log.info(s"Getting Fare for FlightNumber $flightNumber and FlightDate $flightDate")
sender() ! fares.find { fare => (flightNumber == fare.flightNumber & flightDate == fare.flightDate) }
}
}
我也有路线
object FareController extends App with FareJsonProtocol with SprayJsonSupport{
val fareRoutes =
(path("api" / "fare") & get & extractLog) { log =>
parameter(Symbol("flightNumber").as[String],
Symbol("flightDate").as(stringToLocalDate)) { (flightNumber: String, flightDate: LocalDate) =>
log.info(s"Searching for Fare with FlightNumber $flightNumber & FlightDate: $flightDate")
val fareFuture: Future[Option[Fare]] = (fareDb ? GetFare(flightNumber, flightDate)).mapTo[Option[Fare]]
complete(fareFuture)
}
}
}
当我搜索 Fare 并使用 flightNumber 和 flightDate“询问”Actor 时,它 returns 一个 Future[Option[Fare]]。如果是returns一个车费,那就allz好了。我可以只使用 complete(fareFuture) 因为我有 SprayJson。但是如何迎合一个Future[None]呢?。我想为空结果抛出异常并相应地发送 HttpResponse。我们该怎么做?谢谢。
我假设你想 return 404 当你的未来 return 是 None
时。你不需要抛出异常来处理这种情况。
您可以通过以下方式修改路由来处理None
.
而不是 complete(fareFuture)
执行以下操作
onSuccess(fareFuture) {
case Some(fare) => complete(StatusCodes.OK -> fare)
case None => complete(StatusCodes.NotFound)
}
我有案例class
case class Fare(flightNumber: String, flightDate: LocalDate, amount: Int)
和演员
class FareDb extends Actor with ActorLogging {
import FareDb._
var fares: List[Fare] = List()
override def receive: Receive = {
case AddFare(fare) =>
log.info(s"Adding fare $fare")
fares = fares :+ fare
case GetFare(flightNumber, flightDate) =>
log.info(s"Getting Fare for FlightNumber $flightNumber and FlightDate $flightDate")
sender() ! fares.find { fare => (flightNumber == fare.flightNumber & flightDate == fare.flightDate) }
}
}
我也有路线
object FareController extends App with FareJsonProtocol with SprayJsonSupport{
val fareRoutes =
(path("api" / "fare") & get & extractLog) { log =>
parameter(Symbol("flightNumber").as[String],
Symbol("flightDate").as(stringToLocalDate)) { (flightNumber: String, flightDate: LocalDate) =>
log.info(s"Searching for Fare with FlightNumber $flightNumber & FlightDate: $flightDate")
val fareFuture: Future[Option[Fare]] = (fareDb ? GetFare(flightNumber, flightDate)).mapTo[Option[Fare]]
complete(fareFuture)
}
}
}
当我搜索 Fare 并使用 flightNumber 和 flightDate“询问”Actor 时,它 returns 一个 Future[Option[Fare]]。如果是returns一个车费,那就allz好了。我可以只使用 complete(fareFuture) 因为我有 SprayJson。但是如何迎合一个Future[None]呢?。我想为空结果抛出异常并相应地发送 HttpResponse。我们该怎么做?谢谢。
我假设你想 return 404 当你的未来 return 是 None
时。你不需要抛出异常来处理这种情况。
您可以通过以下方式修改路由来处理None
.
而不是 complete(fareFuture)
执行以下操作
onSuccess(fareFuture) {
case Some(fare) => complete(StatusCodes.OK -> fare)
case None => complete(StatusCodes.NotFound)
}