windweller
windweller

Reputation: 2385

Akka Ask Pattern on future recover

I'm writing a Spray + Akka small server. And a blog post stood out and recommended this ask pattern. The CapserOk class has a signiture of this:

case class CasperOk(company: Option[Company.Company], existOrNot: Boolean)

I'm retrieving this database row and if it doesn't exist or some error occured, I want to send out a response from Spray to tell the client.

However, using this pattern, I don't know where to inject the switch.

I tried to change the code to this:

   val response = (secCompanyActor ? jObjectFromCasper(company))
                .mapTo[CasperOk]
                .map(result => result.succeedOrNot match {
                  case true => (OK, "transaction successful")
                  case false => (Conflict, "FileLoc format is wrong or it already exists")
                })
                .recover{case _ => (InternalServerError, "error occured! We will fix this")}

    complete(response)

Spray uses complete() to send out a HTTP response. complete() can either take two objects, or a string/serializable object. I want to use the two object mode (which allows me to manually encode its header), and an ideal situation should look like complete(response._1, response._2)

Is there any way to achieve this with Akka's Future?

Upvotes: 1

Views: 396

Answers (1)

Mustafa Simav
Mustafa Simav

Reputation: 1019

You can achieve this via registering a call to complete function on future onComplete method.

response.onComplete {
  case (statusCode, message) => complete(statusCode, message)
}

Upvotes: 4

Related Questions