mini-effect
    Preparing search index...

    Function failure

    • Creates a tagged error class identified by the given tag string.

      Type Parameters

      • T extends string

        A string literal type used as the tag discriminator

      Parameters

      • tag: T

        A unique string that identifies this error class

      Returns FailureConstructor<T>

      A FailureConstructor — a class whose instances carry the __tag property

      The returned class extends Error and adds a __tag property set to the provided tag. Tagged errors integrate with catchTags to enable type-safe, pattern-matched error recovery.

      Instances are constructed with an optional ErrorOptions (e.g. { cause }) and their message is set to the tag string.

      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"