簡體   English   中英

如何在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知道該異常意味着返回HTTP 404 Not Found。

Spray / akka是否包含我可以手動拋出的某種NotFoundException?

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

      case None => HttpResponse(StatusCodes.NotFound)

您可以引發任何異常,然后配置異常處理程序以將異常轉換為404響應。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM