使用scala生成错误并进行喷涂

时间:2015-07-07 17:22:54

标签: scala spray spray-json spray-dsl

我尝试使用scala和spray-routing创建简单的CRUD应用程序。我有以下路线:

override def receive = runRoute {
    path("entities" / LongNumber) { id =>
      get {
        produce(instanceOf[MyEntity]) {
          func => ctx => heavyLookupById(id, func)
        }
    }
}

我是根据官方文档http://spray.io/documentation/1.2.2/spray-routing/marshalling-directives/produce/

写的

MyEntity如下(实际上并不重要):

case class MyEntity(text: String, id: Option[Long])

我有以下json支持对象

object MyJsonSupport extends DefaultJsonProtocol with SprayJsonSupport {
    implicit val format = jsonFormat2(MyEntity)
}

" heavyLookupById"函数包含一些繁重的阻塞计算(假设数据库查询或http请求),因此我必须处理scala未来:

def heavyLookupById(id: Long, func: MyEntity => Unit) = {
   // ... heavy computations
   future onSuccess { case x => func(x) }
}

但如果我的未来失败,我该怎么办?我想回复一些不好的请求(400)或找不到(404)HTTP错误,但该怎么做?如果我不调用" func"在" heavyLookupById" - 请求挂起 - 我相信默认服务器超时(1分钟左右)会失败。

1 个答案:

答案 0 :(得分:1)

您有RequestContext(ctx),因此您可以调用reject,failWith或RequestContext上可用的任何其他方法。

  val route = path("entities" / LongNumber) { id =>
    get {
      produce(instanceOf[MyEntity]) {
        func => ctx => heavyLookupById(id, func, ctx)
      }
    }
  }

  def heavyLookupById(id: Long, func: MyEntity => Unit, ctx: RequestContext) = {
    // ... heavy computations
    val future = Future.successful(MyEntity("Hello", Some(1)))
    future.onComplete {
      case Success(value) => func(value)
      case Failure(ex) => ctx.failWith(ex)
    }

  }

个人更喜欢handleWith而不是制作,我觉得它更容易阅读。

如果失败,喷雾将返回500,您可以使用exceptionHandlers自定义。

  def heavyLookupById(id: Long) = {
    // ... heavy computations
    Future.successful(MyEntity("Hello", Some(1)))
  }


  val route = path("entities" / LongNumber) { id =>
    get {
      handleWith(heavyLookupById)
    }
  }