<E>(error: E): Channel<never, E, never>Constructs a channel that fails immediately with the specified error.
Example (Failing with an error)
import { Channel } from "effect"
// Create a channel that fails with a string error
const failedChannel = Channel.fail("Something went wrong")
// Create a channel that fails with a custom error
class CustomError extends Error {
constructor(message: string) {
super(message)
this.name = "CustomError"
}
}
const customErrorChannel = Channel.fail(new CustomError("Custom error"))
// Use in error handling by piping to another channel
const channelWithFallback = Channel.concatWith(
failedChannel,
() => Channel.succeed("fallback value")
)export const const fail: <E>(
error: E
) => Channel<never, E, never>
Constructs a channel that fails immediately with the specified error.
Example (Failing with an error)
import { Channel } from "effect"
// Create a channel that fails with a string error
const failedChannel = Channel.fail("Something went wrong")
// Create a channel that fails with a custom error
class CustomError extends Error {
constructor(message: string) {
super(message)
this.name = "CustomError"
}
}
const customErrorChannel = Channel.fail(new CustomError("Custom error"))
// Use in error handling by piping to another channel
const channelWithFallback = Channel.concatWith(
failedChannel,
() => Channel.succeed("fallback value")
)
fail = <function (type parameter) E in <E>(error: E): Channel<never, E, never>E>(error: Eerror: function (type parameter) E in <E>(error: E): Channel<never, E, never>E): 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, function (type parameter) E in <E>(error: E): Channel<never, E, never>E, never> => const fromPull: <
OutElem,
OutErr,
OutDone,
EX,
EnvX,
Env
>(
effect: Effect.Effect<
Pull.Pull<OutElem, OutErr, OutDone, EnvX>,
EX,
Env
>
) => Channel<
OutElem,
Pull.ExcludeDone<OutErr> | EX,
OutDone,
unknown,
unknown,
unknown,
Env | EnvX
>
Creates a Channel from an Effect that produces a Pull.
Example (Creating channels from pulls)
import { Channel, Effect } from "effect"
const channel = Channel.fromPull(
Effect.succeed(Effect.succeed(42))
)
fromPull(import EffectEffect.const succeed: <A>(value: A) => Effect<A>Creates an Effect that always succeeds with a given value.
When to use
Use when an effect should complete successfully with a specific value without any errors
or external dependencies.
Example (Creating a successful effect)
import { Effect } from "effect"
// Creating an effect that represents a successful scenario
//
// ┌─── Effect<number, never, never>
// ▼
const success = Effect.succeed(42)
succeed(import EffectEffect.const fail: <E>(
error: E
) => Effect<never, E>
Creates an Effect that represents a recoverable error.
When to use
Use to explicitly signal a recoverable error in an Effect.
Details
The error keeps propagating unless it is handled. You can handle tagged
errors with functions like
catchTag
or
catchTags
.
Example (Creating a failed effect)
import { Data, Effect } from "effect"
class OperationFailedError extends Data.TaggedError("OperationFailedError")<{}> {}
// ┌─── Effect<never, OperationFailedError, never>
// ▼
const failure = Effect.fail(
new OperationFailedError()
)
fail(error: Eerror)))