Boris
Boris

Reputation: 886

type mismatch error in akka dsl route directive

I'm setting up a rest controller with akka http. The controller parses the url, extracts variables, then calls a service which sends a message to an actor, which then queries a repository and sends the data as a message. I finally got the actor to receive the message and query the repo (after having to chain a series of futures), but now I have an error in the controller that I can't understand:

Error:(58, 41) type mismatch;
 found   : Unit
 required: akka.http.scaladsl.server.RequestContext => 
scala.concurrent.Future[akka.http.scaladsl.server.RouteResult]
path("process" / Segment) { process =>

Does this mean I have to include a complete() somewhere else?

I tried making sure that the actor sends a future as the contents of its message, and that the service returns a future to the controller, because I thought this is the only way to avoid null pointers.

These are my dependencies:

"com.typesafe.akka" %% "akka-http"   % "10.1.8",
"com.typesafe.akka" %% "akka-actor"  % "2.5.22",
"com.typesafe.akka" %% "akka-stream" % "2.5.22",
"com.typesafe.akka" %% "akka-http-spray-json" % "10.1.8"

This is the rest controller:

val processRoute =
path("process" / Segment) { process =>
  withoutRequestTimeout {
    parameters("userName", "limit") { (twitterUserName, limit) =>
      get {
        val processRequest: ProcessRequest = new ProcessRequest(twitterUserName, process, limit.toInt)
        import JsonSupport._
        process match {

          case "shout" =>
            val serviceResult // add more cases in future or call method dynamically
            = processService.shout(processRequest)
            var listOfTweetTexts: List[String] = List[String]()

            serviceResult onComplete {
              case Success(result) =>
                for (tweet <- result.tweets) listOfTweetTexts ::= tweet;
                complete(listOfTweetTexts)
              case Failure(t) =>
                actorSystem.log.error("An error has occurred: " + t.getMessage)
                complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "Say hello to failure"))

            }
          // complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "Say hello to" + limit))
          case _ => complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "Say hello to" + limit))
        }
        complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "Say hello to" + limit))
      }
    }
  }
}
}

Upvotes: 3

Views: 905

Answers (1)

Tim
Tim

Reputation: 27356

You are calling onComplete on a Future which returns Unit. What you want to do is use the Akka onComplete on the Future.

So it should be

onComplete(serviceResult) {

rather than

serviceResult onComplete {

Upvotes: 4

Related Questions