The Applicative type class for this ParallelFuture requires a Semigroup[Throwable],
which can be implemented by merging multiple Throwables into a container Throwable
import scala.util.control.NoStackTrace
caseclass MultipleException(throwableSet: Set[Throwable]) extends Exception("Multiple exceptions found") with NoStackTrace {
overridedef toString: String = throwableSet.mkString(" & ")
}
import scalaz.Semigroup
implicitobject ThrowableSemigroup extends Semigroup[Throwable] {
overridedef append(f1: Throwable, f2: => Throwable): Throwable =
f1 match {
case MultipleException(exceptionSet1) =>
f2 match {
case MultipleException(exceptionSet2) => MultipleException(exceptionSet1 ++ exceptionSet2)
case _: Throwable => MultipleException(exceptionSet1 + f2)
}
case _: Throwable =>
f2 match {
case MultipleException(exceptionSet2) => MultipleException(exceptionSet2 + f1)
case _: Throwable => MultipleException(Set(f1, f2))
}
}
}
Parallel-tagged type of Future that needs to be executed in parallel when using an scalaz.Applicative instance
The Applicative type class for this
ParallelFuturerequires aSemigroup[Throwable], which can be implemented by merging multipleThrowables into a containerThrowableGiven a momorized Future,
and two
ParallelFutures that throw exceptions,and a
Futurethat depends on two String values.When combining those futures together,
then multiple exceptions should be handled together.
futureResult.handleError { case MultipleException(throwables) if throwables.map(_.getMessage) == Set("b failed", "c failed") => Future.now("Multiple exceptions handled") }.map { _ should be("Multiple exceptions handled") }.toScalaFuture