Source(1 to 10)
.map(x ⇒ if (x % 2 == 0) throw new RuntimeException("test error"))
.mapError {
case NonFatal(ex) ⇒
// do something with the failed element 2
ex.printStackTrace()
ex
}
The question is why not mapError give me element ‘2’ along with the ‘RuntimeException’ ?
In case where map is from int to string, we want to know the input ints for which mapping to string failed. With all such failed ints we want do some side effecting like calling a function of type “(int, throwable) => unit”
Here you use two separate stages, map and mapError and the could have been more inbetween. My point is that there is no way to propagate the Int dowstream to mapError.
If you need this control you should use ordinary try-catch inside the map.