Signature
export function catchKind< I, O, E extends TaggedError, C extends object, K extends E["kind"], O2, E2 = never,>( source: Behavior<I, O, E, C>, kind: K, handler: ( error: Extract<E, { kind: K }>, input: I, context: TaskContext<C>, ) => Awaitable<Result<O2, E2>>,): Behavior<I, O | O2, Exclude<E, { kind: K }> | E2, C>;Imports
import { catchKind } from "@/lib/flow/catch-kind";import { catchKind } from "@/lib/flow";Type Parameters
IInput type.
OSource output type.
ETagged source domain error union.
CCustom context type.
KError kind handled by this recovery step.
O2Handler output type.
E2Handler domain error type.
Parameters
kindSpecific `kind` value to recover.
handlerCallback invoked for matching domain errors.
Returns
A behavior with the handled error variant removed unless the handler returns a new error.
Behavior<I, O | O2, Exclude<E, { kind: K }> | E2, C>Details
Matching errors are passed to the handler. Non-matching domain errors remain typed, and runtime
errors bypass the handler.
Examples
const withDefault = catchKind(loadSettings, "missing", (_error, _input, context) => context.ok({ theme: "system" }),);Related Symbols
Represents a value that may be returned directly or through a Promise.
Executable Flow workflow unit.
Creates an identity behavior for starting typed pipelines.
Explicit success-or-failure outcome used throughout Flow.
Runtime context provided to a `task` callback.
Source
export function catchKind< I, O, E extends TaggedError, C extends object, K extends E["kind"], O2, E2 = never,>( source: Behavior<I, O, E, C>, kind: K, handler: ( error: Extract<E, { kind: K }>, input: I, context: TaskContext<C>, ) => Awaitable<Result<O2, E2>>,): Behavior<I, O | O2, Exclude<E, { kind: K }> | E2, C> { return async (input, context) => { const result = await runBehavior(source, input, context);
if (result.ok) { return result; }
if (isRuntimeError(result.error)) { return err(result.error); }
const error = result.error as E & TaggedError;
if (error.kind !== kind) { return err(error as Exclude<E, { kind: K }>); }
return task((_input: I, taskContext: TaskContext<C>) => handler(error as Extract<E, { kind: K }>, input, taskContext), )(input, context); };}