0Pricing
Scala for Backend Engineering & Functional Programming · Lesson

Error and Dependency Channels

Typed errors and env.

Error and Dependency Channels is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Scala for Backend Engineering & Functional Programming learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Two Powerful Channels

Beyond the success value, ZIO[R, E, A] tracks a typed error channel E and a dependency channel R. Both are visible in the type, making failures and requirements explicit at compile time.

Typed Errors

Model domain failures as a sealed hierarchy and put it in E. The compiler then forces you to handle exactly those cases.

import zio._

sealed trait AppError
case object NotFound extends AppError
case object Forbidden extends AppError

val lookup: IO[AppError, String] = ZIO.fail(NotFound)

Recovering with catchAll

catchAll handles every error in E by providing a new effect. After it, the error channel can become Nothing if fully handled.

import zio._

val safe: UIO[String] =
  ZIO.fail("boom").catchAll(e => ZIO.succeed(s"recovered: $e"))

catchSome and orElse

catchSome recovers only matching errors. orElse falls back to another effect on any failure.

import zio._

val prog = ZIO.fail("x").orElse(ZIO.succeed("fallback"))

Either and fold

either turns ZIO[R, E, A] into ZIO[R, Nothing, Either[E, A]]. fold handles both error and success into one value.

import zio._

val label: UIO[String] =
  ZIO.fail(404).fold(e => s"err $e", v => s"ok $v")

Defects vs Failures

ZIO distinguishes failures (typed, expected, in E) from defects (unexpected throwables). ZIO.die raises a defect; defects are not in the E type and represent bugs.

The Environment R

The R channel declares services an effect needs. Access a service with ZIO.service; the requirement appears in R until provided.

import zio._

trait Logger { def log(s: String): UIO[Unit] }

val prog: ZIO[Logger, Nothing, Unit] =
  ZIO.serviceWithZIO[Logger](_.log("hello"))

Defining a Service

A service is a trait plus a ZLayer that builds it. Layers describe how to construct dependencies and can themselves depend on others.

import zio._

case class ConsoleLogger() extends Logger {
  def log(s: String): UIO[Unit] = ZIO.succeed(println(s))
}
object ConsoleLogger {
  val layer: ULayer[Logger] = ZLayer.succeed(ConsoleLogger())
}

trait Logger { def log(s: String): UIO[Unit] }

Providing Dependencies

provide (or provideLayer) satisfies the R requirement, turning ZIO[Logger, E, A] into ZIO[Any, E, A] ready to run.

import zio._

val runnable: UIO[Unit] =
  ZIO.serviceWithZIO[Logger](_.log("hi"))
    .provide(ConsoleLogger.layer)

trait Logger { def log(s: String): UIO[Unit] }
object ConsoleLogger { val layer: ULayer[Logger] = ??? }

Composing Layers

Layers compose horizontally with ++ (combine) and vertically with >>> (feed one layer's output as another's input), building a full dependency graph.

import zio._

// val appLayer = configLayer ++ (dbLayer >>> repoLayer)

Plain Scala Baseline

For contrast, a self-contained Scala program with manual error handling. ZIO encodes the same error in the type and injects dependencies via layers.

object Main {
  def main(args: Array[String]): Unit = {
    val result: Either[String, Int] = Left("NotFound")
    val msg = result.fold(e => s"err $e", v => s"ok $v")
    println(msg)
  }
}

Quick Check

What does the provide method do to a ZIO[Logger, E, A]?

Recap

You worked with both extra channels:

  • Errors: typed E, catchAll, orElse, fold, defects vs failures
  • Dependencies: R, services, ZLayer, provide, layer composition

Next: running ZIO applications.

Frequently asked questions

Is the “Error and Dependency Channels” lesson free?

Yes — the full text of “Error and Dependency Channels” is free to read here on the web, and the Scala for Backend Engineering & Functional Programming course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Scala for Backend Engineering & Functional Programming course, upgrade to CoddyKit PRO.

What will I learn in “Error and Dependency Channels”?

Typed errors and env. You practise Scala for Backend Engineering & Functional Programming with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Scala for Backend Engineering & Functional Programming?

No prior experience is required. Scala for Backend Engineering & Functional Programming on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Error and Dependency Channels” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Scala for Backend Engineering & Functional Programming lesson?

Yes. Every Scala for Backend Engineering & Functional Programming lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. The ZIO Effect
  2. Composing ZIO
  3. Error and Dependency Channels
  4. Running ZIO Apps
← Back to Scala for Backend Engineering & Functional Programming