mini-effect
    Preparing search index...

    Function catchTags

    • Creates a WrapEffect that catches tagged errors by their __tag property.

      Type Parameters

      • CE extends string

        The union of tag strings that this handler covers

      • E extends Effect<any, any, any, any>

        The Effect type returned by each handler

      Parameters

      • handlers: Record<CE, (cause: unknown) => E | undefined>

        A record mapping tag strings to recovery functions. Each function receives the caught error and returns a recovery Effect or undefined to let the error pass through.

      Returns WrapEffect<inferReturn<E>, inferError<E>, Failure<CE>, never, never>

      A WrapEffect that can be used in a pipe chain

      Accepts a record mapping tag strings to handler functions. When the preceding effect fails with a tagged error (created via failure), the handler matching the error's __tag is invoked. If no handler matches, the error propagates unchanged.

      This enables type-safe, pattern-matched error recovery — each branch of the handler record addresses a specific failure type. The caught error types are removed from the resulting effect's error channel.

      import { failure, fn, pipe, catchTags, succeed, run } from "mini-effect";

      const NotFound = failure("NotFound");
      const Unauthorized = failure("Unauthorized");

      const result = await run(
      pipe(
      fn(() => { throw new NotFound(); }),
      catchTags({
      NotFound: () => succeed("fallback"),
      Unauthorized: () => succeed("login"),
      }),
      ),
      );
      // => "fallback"