zio/zio2

Error Models

wefree 2022. 11. 20. 23:12

Errors = failures present in the ZIO type signature ("checked" errors)

Defects = failures that are unrecoverable, unforeseen, NOT present in the ZIO type signature

 

ZIO[R,E,A] can finish with Exit[E,A]

    - Success[A] containing a value

    - Cause[E]

         - Fail[E] containing the error

         - Die(t: Throwable) which was unforeseen

 

  val failedInt: ZIO[Any, String, Int] = ZIO.fail("I failed!")
  val failureCauseExposed: ZIO[Any, Cause[String], Int] = failedInt.sandbox
  val failureCauseHidden: ZIO[Any, String, Int] = failureCauseExposed.unsandbox
  
  val foldWithCause = failedInt.foldCause(
    cause => s"this failed with ${cause.defects}",
    value => s"this succeed with $value"
  )