Skip to main content
Version: 2.x

Exposing the Cause in The Success Channel

Using the ZIO#cause operation we can expose the cause, and then by using ZIO#uncause we can reverse this operation:

trait ZIO[-R, +E, +A] {
def cause: URIO[R, Cause[E]]
def uncause[E1 >: E](implicit ev: A IsSubtypeOfOutput Cause[E1]): ZIO[R, E1, Unit]
}

In the following example, we expose and then untrace the underlying cause:

import zio._

object MainApp extends ZIOAppDefault {
val f1: ZIO[Any, String, Int] =
ZIO.fail("Oh uh!").as(1)

val f2: ZIO[Any, String, Int] =
ZIO.fail("Oh error!").as(2)

val myApp: ZIO[Any, String, (Int, Int)] = f1 zipPar f2

def run = myApp.cause.map(_.untraced).debug
}

Sometimes the ZIO#mapErrorCause operator is a better choice when we just want to map the underlying cause without exposing the cause.

ZIO Chat

🔥 Ignite your ZIO journey with ZIO Chat! 🔥 Ready to unravel the intricacies of ZIO? As your trusty sidekick, I'm here to light the way. No matter if you're wandering through functional forests or traversing effectful landscapes, I've got answers to keep you on track. Your ZIO adventure starts here – ask me anything and let's explore together!