class BaseError
extends Error

Base class for all errors used in Result<T, E>.

Constructors

BaseError(
message: string,
)

Type Parameters

The serialized data type for this error

Static Properties

traceIdGetter: (() => string | undefined) | undefined

Optional callback to provide a trace ID from ambient context (e.g. OpenTelemetry).

Trellis (or other consumers) can set this once at startup so all BaseError instances capture a traceId automatically unless explicitly provided.

Properties

readonly
id: string

Unique identifier for this error instance

abstract
readonly
name: string

Error type name (used for discrimination)

Methods

Helper method to get base serializable fields. Subclasses should use this to build their complete serializable object.

getContext(): Record<string, unknown>

Get the current context object.

protected
getTraceId(): string | undefined

Get the trace ID for this error. Returns the trace ID captured at error creation time.

toJSON(): string

Serializes error to JSON string.

Serializes error to a plain object. Subclasses must implement this to return their specific data type.

withContext(context?: Record<string, unknown>): this

Add contextual information to this error. Useful for adding runtime context like request IDs, user IDs, etc.

withTraceId(traceId: string | undefined): this

Attach a trace ID if one was not captured when the error was created.