Low level directive – unless you’re sure you need to be working on this low-level you might instead want to try the handleRejections directive which provides a nicer DSL for building rejection handlers.
Transforms rejections from the inner route with an immutable.Seq[Rejection] => Future[RouteResult] functiona Function<Iterable<Rejection>, CompletionStage<RouteResult>>.
sourceval authRejectionsToNothingToSeeHere = recoverRejectionsWith { rejections =>Future{// imagine checking rejections takes a longer time:if(rejections.exists(_.isInstanceOf[AuthenticationFailedRejection]))Complete(HttpResponse(entity ="Nothing to see here, move along."))elseRejected(rejections)}}
val neverAuth:Authenticator[String]= creds =>None
val route =
authRejectionsToNothingToSeeHere {
pathPrefix("auth"){
path("never"){
authenticateBasic("my-realm", neverAuth){ user =>
complete("Welcome to the bat-cave!")}}}}// tests:Get("/auth/never")~> route ~> check {
status shouldEqual StatusCodes.OK
responseAs[String] shouldEqual "Nothing to see here, move along."}