Spray - complete with HTTP code based on Left-Right in Either

I have a design similar to the following

    handleWith {
               mr: MyRequest =>
                (myactor ? mr).mapTo[Either[BadRequest, GoodResponse]]

             }

Based on the results of Either, I would like to fill out either 200 based on the Right answer, and a kind of 4XX based on the left. I'm not quite sure how to pull the future into a match to do this, though.

+4
source share
1 answer

If I interpret your question correctly, do you want to know how the template matches the value Either? If so, you can do something like:

handleWith {
  mr: MyRequest =>
   (myactor ? mr).mapTo[Either[BadRequest, GoodResponse]] match {
     case Left(badRequest) => someSortOf4xxx(badRequest)
     case Right(goodResponse) => anOkResponse(goodResponse)
   }
}
+3
source

All Articles