Genel bakış

Fail

Create a behavior that always fails with a typed domain error.

resulttyped-errors

Install

$
Terminal window
pnpm dlx shadcn@latest add https://archyn.net/r/fail.json

Copy the files below into the target paths shown in each tab.

ts@lib/flow/fail.ts
import { type Behavior } from "./core/runtime";
import { task } from "./task";
/**
* Creates a behavior that always fails with the same domain error.
*
* Use `fail` for fallback branches, tests, and workflow construction where a known typed failure is
* the intended result.
*
* @typeParam E - Domain error type.
* @param error - Error value returned for every execution.
* @returns A behavior that ignores input and returns `Err`.
*
* @example
* ```ts
* const unavailable = fail({ kind: "offline" });
* ```
*/
export function fail<E>(error: E): Behavior<unknown, never, E> {
return task<unknown, never, E>((_input, context) => context.fail(error));
}

Update the import paths to match your project setup.

Create a Behavior that always fails with a fixed typed domain error.

Usage

fail is the failure counterpart to succeed. It is useful for branches, tests, and explicit guard Behavior.

Use it when:

  • Model an always-failing branch.
  • Return a fixed guard error from a composition.
  • Test recovery and error mapping Behavior.
import { fail } from "@/lib/flow/fail";
const requireAuthentication = fail({ kind: "unauthenticated" as const });

Behavior

fail wraps the error in a task and returns context.fail(error) for every run.

The fixed error is a typed domain failure, not a thrown exception. Runtime cancellation can still stop execution before the task body runs.

Types

The behavior has unknown input, never output, and the provided domain error type.

Import from @/lib/flow/fail.

Composition Notes

Use fail for constant negative branches and tests.

Do not use fail for unexpected exceptions; throw and let the runtime classify them as Thrown.

Source

ts@lib/flow/fail.ts
import { type Behavior } from "./core/runtime";
import { task } from "./task";
/**
* Creates a behavior that always fails with the same domain error.
*
* Use `fail` for fallback branches, tests, and workflow construction where a known typed failure is
* the intended result.
*
* @typeParam E - Domain error type.
* @param error - Error value returned for every execution.
* @returns A behavior that ignores input and returns `Err`.
*
* @example
* ```ts
* const unavailable = fail({ kind: "offline" });
* ```
*/
export function fail<E>(error: E): Behavior<unknown, never, E> {
return task<unknown, never, E>((_input, context) => context.fail(error));
}