Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala Akka HighLevelHttp从恢复未来返回路由_Scala_Routes_Akka_Future_Akka Http - Fatal编程技术网

Scala Akka HighLevelHttp从恢复未来返回路由

Scala Akka HighLevelHttp从恢复未来返回路由,scala,routes,akka,future,akka-http,Scala,Routes,Akka,Future,Akka Http,因为我有一个请求主体,所以我需要将其作为将来的请求,并在操作之后发送响应。但是,我需要检查主体是否有效,如果无效,我想返回一个BADDREQUEST状态 我知道代码太多,所以请查看requestFuture,恢复功能中的问题 (pathPrefix("api" / "search") & post & extractRequest) { request => val tokenResult = request.headers.fi

因为我有一个请求主体,所以我需要将其作为将来的请求,并在操作之后发送响应。但是,我需要检查主体是否有效,如果无效,我想返回一个BADDREQUEST状态

我知道代码太多,所以请查看
requestFuture
,恢复功能中的问题

(pathPrefix("api" / "search") & post & extractRequest) { request =>
  val tokenResult = request.headers.find(x => x.name.toLowerCase == "token")
    tokenResult match {
      case None => throw new IllegalArgumentException(ServerMessages.TOKEN_NOT_FOUND)
      case Some(token) => token.value match {
        case this.apiToken => {
          val entity = request.entity
          val strictEntityFuture = entity.toStrict(2 seconds)
          val requestFuture = strictEntityFuture
              .map(_.data.utf8String.parseJson
                .convertTo[SearchFilters])

          requestFuture map { filters =>
             // here - I return an Route with data from controller
             complete(controller.searchData(filters)
               .map(_.toJson.prettyPrint)
               .map(toHttpEntity)))
          } recover (e => {
             // HERE IS THE PROBLEM
             // I need to return an Route, but here will be a Future[Route]
             complete(400 -> ServerMessages.INVALID_REQUEST_BODY)
          }
        }
        case _ => throw new IllegalArgumentException(ServerMessages.INVALID_TOKEN)
      }
}
我需要从未来解压响应,或者使用另一种方式抛出错误

找到了一个使用
onComplete
指令的解决方案,但我需要知道我的json是否成功转换,以便抛出自定义错误

onComplete(requestFuture) {
    case Success(filters) => searchKpi(filters) ~
       pathEndOrSingleSlash {
           complete(403 -> ServerMessages.INVALID_METHOD_ARGUMENTS)
       }
    case Failure(ex) => failWith(ex) // returns 500 Internal Server Error
}
谢谢

试试以下方法:

val requestFuture = strictEntityFuture

requestFuture.map { entity =>
  entity.data.utf8String match {
  // Do your processing here
  case "" => RouteResult.Complete(HttpResponse(StatusCode.int2StatusCode(200), entity = HttpEntity("OK")))
}}.recover {
  case err: Throwable =>
  RouteResult.Complete(HttpResponse(StatusCode.int2StatusCode(500), entity = HttpEntity(err.getMessage)))
}
你可以使用

像在文档中一样定义异常处理程序,并将异常(应该与
failWith
引发的异常相同)添加到需要返回的HTTP状态(您提到
BadRequest

请注意,有两种方法可以附加异常处理程序

从文档:


我已经有了一个
异常句柄
r,但只有两个案例(
IllegalArgument
NoSuchElement
)。由于我扩展了我的处理程序,可以返回
BadRequest
响应。但是我在那里做了一些错误的事情,因为我的
BadRequestException
被打包到
defaultExceptionHandler
路由


您可以执行类似于处理异常(NoTouchElementExceptionHandler.withFallback(tokenNotFoundExceptionHandler)。withFallback(defaultExceptionHandler))的操作。
    case class MyBadException() extends RuntimeException

  implicit def myExceptionHandler: ExceptionHandler =
    ExceptionHandler {
      case MyBadException() => complete(HttpResponse(BadRequest)) // You decide the HTTP status to return
      case _ => complete(HttpResponse(InternalServerError, entity = "Bad numbers, bad result!!!"))
    }


    val route = path("test") {
      onComplete(getSomething()) {
        case Success(v) => complete("Ok")
        case Failure(err) => failWith(MyBadException())
      }
    }

Bring it into implicit scope at the top-level.
Supply it as argument to the handleExceptions directive.
val routes = (
    handleExceptions(noSuchElementExceptionHandler) &
      handleExceptions(tokenNotFoundExceptionHandler)) {
    domainRoutes
  }

private val domainRoutes = {
   onComplete(future) {
      case Success(values) => complete(controller.getdata(values))
      case Failure(e) => failWith(BadRequestExceptionHandler(e.getMessage))
   }
}