def handleResponse()

in app/utils/AkkaHttpHelpers.scala [71:106]


  def handleResponse(response:HttpResponse, description:String) (implicit mat:Materializer, ec:ExecutionContext):Future[Either[HttpUnsuccessActions, Option[HttpEntity]]] = response.status.intValue() match {
    case 200 =>
      Future(Right(Some(response.entity)))
    case 404 =>
      response.entity.discardBytes()
      Future(Right(None))
    case 403|401 =>
      response.entity.discardBytes()
      Future.failed(new RuntimeException(s"$description said permission denied."))
    case 409 =>
      consumeResponseEntity(response.entity)
        .flatMap(body => Future.failed(new RuntimeException(s"$description returned a conflict error: $body")))
    case 400 =>
      consumeResponseEntity(response.entity)
        .flatMap(body => Future.failed(new RuntimeException(s"$description returned bad data error: $body")))
    case 301 |302|303|308|309=>
      response.entity.discardBytes()
      logger.info(s"Received unexpected redirect from $description to ${response.getHeader("Location")}")
      val h = response.getHeader("Location")
      if (h.isPresent) {
        Future(Left(RedirectRequired(h.get().value())))
      } else {
        Future.failed(new RuntimeException(s"$description returned an Unexpected redirect without location"))
      }
    case 500 | 502 | 503 | 504 =>
      consumeResponseEntity(response.entity).map(body=>{
        logger.error(s"$description returned a server error ${response.status}: $body. Retrying...")
        Left(RetryRequired)
      })
    case _=>
      consumeResponseEntity(response.entity)
        .flatMap(body=>{
          logger.error(s"Received unexpected response ${response.status} from $description, with content $body")
          Future.failed(new RuntimeException(s"$description returned unexpected response: ${response.status}"))
        })
  }