@tigorlazuardi/otel-cloudflare - v1.1.1
    Preparing search index...

    Class TracedError

    Custom error class with tracing support and HTTP status codes.

    Features:

    • Captures caller info automatically
    • Supports HTTP status codes via ErrorStatus interface
    • Can wrap other errors while preserving the cause chain
    • Custom instanceof behavior that checks the cause chain
    • JSON serialization via toJSON()
    // Create a new error
    throw TracedError.fail("User not found", {
    status: ErrorStatus.NotFound,
    fields: { userId: 123 }
    });
    // Wrap an existing error
    try {
    await fetchUser(id);
    } catch (err) {
    throw TracedError.wrap(err, "Failed to fetch user", {
    status: ErrorStatus.InternalServerError,
    fields: { userId: id }
    });
    }

    Hierarchy

    • Error
      • TracedError
    Index

    Properties

    caller: CallerInfo
    cause?: unknown
    fields: Record<string, unknown>
    status: ErrorStatus

    Methods

    • Check if this error or any error in the cause chain is an instance of the given class.

      Type Parameters

      • T extends Error

      Parameters

      • ErrorClass: new (...args: unknown[]) => T

        The error class to check against

      Returns this is TracedError & { cause: T }

      true if this error or any wrapped error is an instance of ErrorClass

      const wrapped = TracedError.wrap(new TypeError("bad"), "context");
      wrapped.is(TypeError) // true
      wrapped.is(RangeError) // false
    • Get the root cause (the innermost wrapped value)

      Returns unknown

    • Serialize error to JSON

      When cause is a TracedError, it calls toJSON() recursively to avoid double printing stack traces.

      Returns Record<string, unknown>

    • Custom instanceof behavior - also matches wrapped errors in the cause chain.

      This allows:

      const wrapped = TracedError.wrap(new TypeError("bad"), "context");
      wrapped instanceof TracedError // true

      And when checking the cause chain with is():

      wrapped.is(TypeError) // true
      

      Parameters

      • instance: unknown

      Returns boolean

    • Wrap an existing error with additional context

      Parameters

      • cause: unknown

        The cause to wrap (can be any value)

      • message: string

        New message describing the context

      • Optionaloptions: TracedErrorOptions

        Optional status, fields, and caller info

      Returns TracedError