0
votes

I am using logRequestResponse debugging directive in order to log every request/response failing through whole path tree. Log entries looks as follows:

2015-07-29 14:03:13,643 [INFO ] [DataImportServices-akka.actor.default-dispatcher-6] [akka.actor.ActorSystemImpl]ActorSystem(DataImportServices) - get-userr: Response for
  Request : HttpRequest(POST,https://localhost:8080/city/v1/transaction/1234,List(Accept-Language: cs, Accept-Encoding: gzip,...
  Response: Rejected(List(MalformedRequestContentRejection(Protocol message tag had invalid wire type.,...

My root route trait which assembles all partial routes to one look as follows:

trait RestAPI extends Directives {
  this: ServiceActors with Core =>

  private implicit val _ = system.dispatcher

  val route: Route =
      logRequestResponse("log-activity", Logging.InfoLevel) {
        new CountryImportServiceApi().route ~
        new CityImportServiceApi().route
  }
}

And partial routes are defined as following:

class CinemaImportServiceApi()(implicit executionContext: ExecutionContext) extends Directives {

  implicit val timeout = Timeout(15 seconds)    

  val route: Route = {
    pathPrefix("city") {
      pathPrefix("v1") {
        path("transaction" / Segment ) {
          (siteId: String, transactionId: String) =>
            post {
              authenticate(BasicAuth(cityUserPasswordAuthenticator _, realm = "bd city import api")) {
                user =>
                  entity(as[CityTrans]) { e =>
                    complete {
                      StatusCodes.OK

                    }
                  }
              }
            }
        }
      }
    }
  }

Assembled routes are run via HttpServiceActor runRoute.

I would like to convert rejection to StatusCode and log that via logRequestResponse. Even though I write a custom function for logging I get rejection. What seems to me fishy is that since it is wrapped the whole route tree rejection is still not converted to HttpResponse. In tests we are sealing the route in order to convert Rejection to HttpResponse. Is there a way how to mark a route as a complete route hence actually seal it? Or am I missing some important concept here?

Thx

1

1 Answers

0
votes

I would add something like the following:

} ~ // This is the closing brace of the pathPrefix("city"), just add the ~
pathPrefix("") {
  complete {
    (StatusCodes.OK, "Invalid Route")
  }
}

of course, change the OK and the body to whatever you need. pathPrefix("") will match any path that was rejected by any previous routes.