One way of solving this is to define a fan-out stage partitioning the Try
into 2 streams, depending on its outcome
object PartitionTry {
def apply[T]() = GraphDSL.create[FanOutShape2[Try[T], Throwable, T]]() { implicit builder ⇒
import GraphDSL.Implicits._
val success = builder.add(Flow[Try[T]].collect { case Success(a) ⇒ a })
val failure = builder.add(Flow[Try[T]].collect { case Failure(t) ⇒ t })
val partition = builder.add(Partition[Try[T]](2, _.fold(_ ⇒ 0, _ ⇒ 1)))
partition ~> failure
partition ~> success
new FanOutShape2[Try[T], Throwable, T](partition.in, failure.out, success.out)
}
}
Then your generic flow can ingest Try
s and send the Failure
s to a sink of choice, whilst passing the Success
es on
object ErrorHandlingFlow {
def apply[T, MatErr](errorSink: Sink[Throwable, MatErr]): Flow[Try[T], T, MatErr] = Flow.fromGraph(
GraphDSL.create(errorSink) { implicit builder ⇒ sink ⇒
import GraphDSL.Implicits._
val partition = builder.add(PartitionTry[T]())
partition.out0 ~> sink
new FlowShape[Try[T], T](partition.in, partition.out1)
}
)
}
usage example below
val source : Source[String, NotUsed] = Source(List("1", "2", "hello"))
val convert : Flow[String, Try[Int], NotUsed] = Flow.fromFunction((s: String) ⇒ Try{s.toInt})
val errorsSink : Sink[Throwable, Future[Done]] = Sink.foreach[Throwable](println)
val handleErrors: Flow[Try[Int], Int, Future[Done]] = ErrorHandlingFlow(errorsSink)
source.via(convert).via(handleErrors).runForeach(println)
Note that
- the 2 stages defined above are reusable for any type (write once, use everywhere)
- this approach can be reused for other type classes - like
Either
, etc.