(defect: unknown): Channel<never, never, never>Constructs a channel that fails immediately with the specified defect.
Example (Dying with defects)
import { Channel } from "effect"
// Create a channel that dies with a string defect
const diedChannel = Channel.die("Unrecoverable error")
// Create a channel that dies with an Error object
const errorDefect = Channel.die(new Error("System failure"))
// Die with any value as a defect
const objectDefect = Channel.die({
code: "SYSTEM_FAILURE",
details: "Critical system component failed"
})export const const die: (
defect: unknown
) => Channel<never, never, never>
Constructs a channel that fails immediately with the specified defect.
Example (Dying with defects)
import { Channel } from "effect"
// Create a channel that dies with a string defect
const diedChannel = Channel.die("Unrecoverable error")
// Create a channel that dies with an Error object
const errorDefect = Channel.die(new Error("System failure"))
// Die with any value as a defect
const objectDefect = Channel.die({
code: "SYSTEM_FAILURE",
details: "Critical system component failed"
})
die = (defect: unknowndefect: unknown): interface Channel<out OutElem, out OutErr = never, out OutDone = void, in InElem = unknown, in InErr = unknown, in InDone = unknown, out Env = never>A Channel is a nexus of I/O operations, which supports both reading and
writing. A channel may read values of type InElem and write values of type
OutElem. When the channel finishes, it yields a value of type OutDone. A
channel may fail with a value of type OutErr.
Details
Channels are the foundation of Streams: both streams and sinks are built on
channels. Most users shouldn't have to use channels directly, as streams and
sinks are much more convenient and cover all common use cases. However, when
adding new stream and sink operators, or doing something highly specialized,
it may be useful to use channels directly.
Channels compose in a variety of ways:
- Piping: One channel can be piped to another channel, assuming the
input type of the second is the same as the output type of the first.
- Sequencing: The terminal value of one channel can be used to create
another channel, and both the first channel and the function that makes
the second channel can be composed into a channel.
- Concatenating: The output of one channel can be used to create other
channels, which are all concatenated together. The first channel and the
function that makes the other channels can be composed into a channel.
Example (Typing channels)
import type { Channel } from "effect"
// A channel that outputs numbers and requires no environment
type NumberChannel = Channel.Channel<number>
// A channel that outputs strings, can fail with Error, completes with boolean
type StringChannel = Channel.Channel<string, Error, boolean>
// A channel with all type parameters specified
type FullChannel = Channel.Channel<
string, // OutElem - output elements
Error, // OutErr - output errors
number, // OutDone - completion value
number, // InElem - input elements
string, // InErr - input errors
boolean, // InDone - input completion
{ db: string } // Env - required environment
>
Channel<never, never, never> => const failCause: <E>(
cause: Cause.Cause<E>
) => Channel<never, E, never>
Constructs a channel that fails immediately with the specified Cause.
When to use
Use when the channel failure must preserve a full Cause, such as defects,
interruptions, or combined failures.
Example (Failing with causes)
import { Cause, Channel } from "effect"
// Create a channel that fails with a simple cause
const simpleCause = Cause.fail("Simple error")
const failedChannel = Channel.failCause(simpleCause)
// Create a channel with a die cause
const dieCause = Cause.die(new Error("System error"))
const dieFailure = Channel.failCause(dieCause)
// Create a channel with a simple fail cause
const failCause = Cause.fail("Simple error")
const simpleFail = Channel.failCause(failCause)
failCause(import CauseCause.die(defect: unknowndefect))