我的登录端点看起来大致像这样
get {
(path("token") & parameters("email", "password")) { (email, password) =>
complete {
DBManager.getUserByEmail(email) match {
case Some(user) =>
// Check everything is return something
UserWire(user)
case None => StatusCodes.NotFound -> "User doesn't exist"
}
}
}
}
DBManager.getUserByEmail
返回Option[User]
。我刚刚切换到 Slick,其中一切都是异步的,因此方法现在返回Future[User]
。
未来失败时如何发送所需的响应?我试过这个
complete {
DBManager.getUserByEmail(email).map(user => {
// Check everything is return something
UserWire(user)
}).recoverWith { case ex => Future.successful(StatusCodes.NotFound -> "User doesn't exist") }
}
它无法编译
Error:(497, 26) type mismatch;
found : scala.concurrent.Future[Product with Serializable]
required: akka.http.scaladsl.marshalling.ToResponseMarshallable
}).recoverWith { case ex => Future.successful(StatusCodes.NotFound -> "User doesn't exist") }
^
我该如何解决?