CmpStr - v3.3.0
    Preparing search index...

    Class ErrorUtil

    Helper utilities for throwing and formatting errors.

    Provides methods for asserting conditions, wrapping unknown errors, and formatting errors into readable strings. This centralizes error handling logic and ensures consistent error messages across the codebase.

    Index

    Constructors

    Methods

    • Throw a CmpStrUsageError if a condition is not met.

      Parameters

      • condition: boolean

        The condition to assert

      • message: string

        The error message to throw if the condition is false

      • Optionalmeta: CmpStrErrorMeta

        Optional structured metadata for the error

      Returns asserts condition

      • If the condition is false
    • Format any error into a readable string.

      Parameters

      • err: unknown

        The error to format

      Returns string

      • A formatted string representation of the error
    • Wrap an unknown error into a CmpStrInternalError.

      Parameters

      • err: unknown

        The error to wrap

      • message: string

        The error message to use for the wrapped error

      • Optionalmeta: CmpStrErrorMeta

        Optional structured metadata for the error

      Returns never

      • Always throws a new CmpStrInternalError wrapping the original error
    • Execute a synchronous operation and wrap any exception as a CmpStrInternalError.

      This is used to avoid repeating try/catch blocks and to add consistent context to unexpected failures while preserving the original error as cause.

      Type Parameters

      • T

      Parameters

      • fn: () => T

        The function to execute

      • message: string

        The error message to use if an exception is thrown

      • Optionalmeta: CmpStrErrorMeta

        Optional structured metadata for the error

      Returns T

      The result of the function if it executes successfully

      • If the function throws an error, it will be wrapped and re-thrown as a CmpStrInternalError
    • Execute an asynchronous operation and wrap any exception as a CmpStrInternalError.

      Type Parameters

      • T

      Parameters

      • fn: () => Promise<T>

        The asynchronous function to execute

      • message: string

        The error message to use if an exception is thrown

      • Optionalmeta: CmpStrErrorMeta

        Optional structured metadata for the error

      Returns Promise<T>

      A promise that resolves to the result of the function if it executes successfully

      • If the function throws an error, it will be wrapped and re-thrown as a CmpStrInternalError