如何在 spray/akka 中手动抛出 HTTP 404 Not Found 异常?

How to manually throw HTTP 404 Not Found exception in spray/akka?

在我的存储库函数中,我正在读取用户,然后更新该用户:

def update(u: User): Future[Int] = {
    this.read(u.id).flatMap {
      case Some(existingUser) =>
        db.run(
          userTable
            .filter(_.id === user.id)
            .update(user.copy(createdDate = existingUser.createdDate)))
      //case None => throw new NotFoundException(); // does this exception exist in spray/akka?
    }
}

我想在找不到用户时在这里抛出某种异常,这样 spray/akka 就会知道异常意味着 return HTTP 404 Not Found。

spray/akka 是否包含某种我可以手动抛出的 NotFoundException?

它是否必须是一个例外,或者你可以使用这个:

      case None => HttpResponse(StatusCodes.NotFound)

您可以抛出任何异常,然后配置一个 exception handler 将异常转换为 404 响应。