title: Handling Retry Logic on HttpResponse 404 with Akka http
body: <p>I am trying to implement a code which makes request to external REST endpoint and when that endpoint returns a 404, it should retry for finite times.</p>
  
  <p>The HttpRequest is something like</p>
  
  <pre><code>val responseFuture = Http().singleRequest(HttpRequest(method = requestMethod,
      uri = url,
      entity = HttpEntity(requestBody).withContentType(ContentTypes.`application/json`)
  ))
  </code></pre>
  
  <p>and the response is handled as</p>
  
  <pre><code>responseFuture.onComplete {
      case Success(r) =&gt;
        if (r.status.isFailure()) Future.failed(new Exception("request failed with status 404"))
        else r
      case Failure(e) =&gt; throw e
  }
  </code></pre>
  
  <p>My retry logic is:</p>
  
  <pre><code>def retryFuture[T](retries: Int, delay: FiniteDuration = 1.second)(fn: =&gt; Future[T])(implicit ec: ExecutionContext, s: Scheduler): Future[T] = {
      fn.recoverWith {
        case _ if retries &gt; 0 =&gt; after(delay, s)(retryFuture(retries - 1, delay)(fn))
      }
  }
  </code></pre>
  
  <p>The problem is when the endpoint returns 404, it comes as SUCCESS(HttpResponse(404,...) and so the retry is not working. Can anyone point out what can be done to resolve this?</p>