简体   繁体   English

如何在Spray / akka中手动引发HTTP 404 Not Found异常?

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

In my repository function, I'm reading a User, then updating that user: 在我的存储库功能中,我正在读取一个用户,然后更新该用户:

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?
    }
}

I'd like to throw some sort of exception here when the user is not found, so that spray/akka will know that exception means to return HTTP 404 Not Found. 当找不到用户时,我想在这里抛出某种异常,以便spray / akka知道该异常意味着返回HTTP 404 Not Found。

Does spray/akka contain some sort of NotFoundException that I can manually throw? Spray / akka是否包含我可以手动抛出的某种NotFoundException?

是否一定是例外,或者您可以使用以下方法:

      case None => HttpResponse(StatusCodes.NotFound)

您可以引发任何异常,然后配置异常处理程序以将异常转换为404响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM