假设:
import scalaz.concurrent.Task
import scalaz._
val result: Task[String \/ Int] = Task { throw new RuntimeException("!")}
然后,我运行它来获得RuntimeException
:
scala> result.unsafePerformSync
java.lang.RuntimeException: !
...
Task
是否有“等效”Future#recover
,例如以下内容?
def recover[A, B, C](t: Task[A], handleError: Throwable => B \/ C]: Task[B \/ C]
答案 0 :(得分:2)
Task#handle
(或者handleWith
)可以解决问题
def f: String = {
throw new RuntimeException()
"I will fail"
}
Task(f).handle{ case _ ⇒ "recovered"}.run \\ recovered