spray 处理 response 的通用函数

def handleActorResponse: PartialFunction[Try[Any],  (StatusCode, ResponseResult)] = {

    case Failure(exp) =>
      (InternalServerError, ResponseResult(ResponseResult.StatFailure, exp.toString))

    case Success(res) =>

      val ret = res.asInstanceOf[ResponseResult]
      
      log.info("response is: " + ret)

      ret.status match {

        case ResponseResult.StatSuccess => (OK, ret)

        case _ => (InternalServerError, ret)

      }
  }

  def completeResponse(arg: (StatusCode, ResponseResult)) = {
    complete(arg._1, arg._2.toString)
  }

//  @todo change ResponseResult to Any for better flexibility

  def sendResponse(response: Future[Any]) = {
    onComplete(response) {
      handleActorResponse andThen completeResponse
    }
  }

  

你可能感兴趣的:(spray 处理 response 的通用函数)