Skip to content

Commit

Permalink
Elasticsearch: Handle failed Future when invoking endpoint in Source (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoballekens authored Oct 1, 2021
1 parent c76657c commit 0de667e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private[elasticsearch] final class ElasticsearchSourceLogic[T](
request,
settings.connection
)
.map {
.flatMap {
case HttpResponse(StatusCodes.OK, _, responseEntity, _) =>
Unmarshal(responseEntity)
.to[String]
Expand All @@ -169,6 +169,9 @@ private[elasticsearch] final class ElasticsearchSourceLogic[T](
.invoke(new RuntimeException(s"Request failed for POST $uri, got $status with body: $body"))
}
}
.recover {
case cause: Throwable => failureHandler.invoke(cause)
}
} else {
log.debug("Fetching next scroll")

Expand All @@ -187,16 +190,22 @@ private[elasticsearch] final class ElasticsearchSourceLogic[T](
request,
settings.connection
)
.map {
.flatMap {
case HttpResponse(StatusCodes.OK, _, responseEntity, _) =>
Unmarshal(responseEntity)
.to[String]
.map(json => responseHandler.invoke(json))
case HttpResponse(status, _, responseEntity, _) =>
Unmarshal(responseEntity).to[String].map { body =>
failureHandler
.invoke(new RuntimeException(s"Request failed for POST $uri, got $status with body: $body"))
}
Unmarshal(responseEntity)
.to[String]
.map { body =>
failureHandler.invoke(
new RuntimeException(s"Request failed for POST $uri, got $status with body: $body")
)
}
}
.recover {
case cause: Throwable => failureHandler.invoke(cause)
}
}
} catch {
Expand Down Expand Up @@ -304,7 +313,7 @@ private[elasticsearch] final class ElasticsearchSourceLogic[T](

ElasticsearchApi
.executeRequest(request, settings.connection)
.map {
.flatMap {
case HttpResponse(StatusCodes.OK, _, responseEntity, _) =>
Unmarshal(responseEntity)
.to[String]
Expand All @@ -317,6 +326,9 @@ private[elasticsearch] final class ElasticsearchSourceLogic[T](
.invoke(Failure(new RuntimeException(s"Request failed for POST $uri, got $status with body: $body")))
}
}
.recover {
case cause: Throwable => failureHandler.invoke(cause)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (C) 2016-2020 Lightbend Inc. <https://www.lightbend.com>
*/

package akka.stream.alpakka.elasticsearch.impl

import akka.actor.ActorSystem
import akka.http.scaladsl.{Http, HttpExt}
import akka.stream.Materializer
import akka.stream.alpakka.elasticsearch._
import akka.stream.alpakka.testkit.scaladsl.LogCapturing
import akka.stream.scaladsl.{Keep, Source}
import akka.stream.testkit.scaladsl.TestSink
import akka.testkit.TestKit
import org.scalatest.BeforeAndAfterAll
import org.scalatest.wordspec.AnyWordSpecLike

import scala.concurrent.ExecutionContext.Implicits.global

class ElasticsearchSourcStageTest
extends TestKit(ActorSystem("elasticsearchSourceStagetest"))
with AnyWordSpecLike
with BeforeAndAfterAll
with LogCapturing {

implicit val mat: Materializer = Materializer(system)
implicit val http: HttpExt = Http()

"ElasticsearchSourceStage" when {
"client cannot connect to ES" should {
"stop the stream" in {
val downstream = Source
.fromGraph(
new impl.ElasticsearchSourceStage[String](
ElasticsearchParams.V7("es-simple-flow-index"),
Map("query" -> """{ "match_all":{}}"""),
ElasticsearchSourceSettings(ElasticsearchConnectionSettings("http://wololo:9202")),
(json: String) => ScrollResponse(Some(json), None)
)
)
.toMat(TestSink.probe)(Keep.right)
.run()

downstream.request(1)
downstream.expectError()
}
}
}

override def afterAll(): Unit = {
super.afterAll()
TestKit.shutdownActorSystem(system)
}
}

0 comments on commit 0de667e

Please sign in to comment.