Skip to content
Docs menu / Yieldable Errors

Yieldable Errors

Errors created with Data.Error and Data.TaggedError are yieldable. Inside Effect.gen, yielding one is equivalent to passing it to Effect.fail.

Data.Error

Use Data.Error when the error does not need a discriminant tag.

Example (Yielding a Custom Error)

import { Data, Effect, Exit } from "effect"
class InvalidInput extends Data.Error<{
readonly message: string
}> {}
const program = Effect.gen(function* () {
return yield* new InvalidInput({ message: "Name is required" })
})
Effect.runSyncExit(program) // => Exit.fail(new InvalidInput({ message: "Name is required" }))

Data.TaggedError

Data.TaggedError adds a readonly _tag field. Tagged errors form discriminated unions that can be handled precisely with Effect.catchTag and Effect.catchTags.

Example (Handling Tagged Errors)

import { Data, Effect } from "effect"
class NotFound extends Data.TaggedError("NotFound")<{
readonly id: string
}> {}
class PermissionDenied extends Data.TaggedError("PermissionDenied")<{
readonly id: string
}> {}
const loadUser = (
id: string,
): Effect.Effect<string, NotFound | PermissionDenied> =>
Effect.gen(function* () {
if (id === "missing") {
return yield* new NotFound({ id })
}
return `user:${id}`
})
const program = loadUser("missing").pipe(
Effect.catchTag("NotFound", (error) => Effect.succeed(`No user ${error.id}`)),
)
Effect.runSync(program) // => "No user missing"

Use tagged errors for domain errors that callers may need to distinguish. The class is both the error’s constructor and its TypeScript type.