Rxova
Skip to content

API reference

Everything below is exported from the package root. There are no subpath exports other than ./package.json.

import {
ExtendedError,
isExtendedError,
defineError,
causeChain,
rootCause,
findCause,
findCauseOf,
hasCauseOf,
serializeError,
deserializeError,
toError,
isErrorLike,
describeValue,
} from 'ts-extended-errors'
ExportKindWhat it does
ExtendedErrorclassBase class: name, code, context, cause, stack, toJSON()
isExtendedErrorfunctionvalue instanceof ExtendedError, as a type guard
defineErrorfunctionReturns a new error class
causeChainfunction[error, error.cause, …], outermost first
rootCausefunctionThe last value in the chain
findCausefunctionThe first value in the chain a predicate accepts
findCauseOffunctionThe first instance of a class in the chain, typed as that class
hasCauseOffunctionWhether the chain contains an instance of a class
serializeErrorfunctionA JSON-safe object for any thrown value
deserializeErrorfunctionRebuilds serializeError output as instances of the original classes
toErrorfunctionAn Error for any value; errors are returned unchanged
isErrorLikefunctionWhether a value is an object with a string message
describeValuefunctionA one-line string for any value

The exported types are listed separately in Types.

class ExtendedError<Context extends ErrorContext = ErrorContext> extends Error {
static readonly code: string | undefined
constructor(message: string, options?: ExtendedErrorOptions<Context>)
}
OptionTypeSets
causeunknownerror.cause, the native Error cause
contextContexterror.context, typed by the class’s Context
MemberValue
nameThe name of the class that was constructed
codeThe constructed class’s static code, or undefined
contextoptions.context, or undefined
causeoptions.cause. The property exists only when a cause was passed
stackStarts at the line that created the error, not inside the constructor
toJSON()serializeError(this), so JSON.stringify(error) includes every field

Declare code as a static override readonly field in a subclass. See Subclassing Error for what the constructor does and why.

function isExtendedError(value: unknown): value is ExtendedError

instanceof, so it is false across two copies of the package in one process, and false for a class built on a base that is not an ExtendedError.

function defineError<Context extends ErrorContext = ErrorContext>(
name: string,
options?: DefineErrorOptions<Context>,
): ExtendedErrorConstructor<Context>
OptionTypeEffect
codestringThe class’s code. Omitted, it inherits the base’s
basean error classThe class to extend. Defaults to ExtendedError
message(context: Context) => stringWrites the message from context; the throw site passes only options

Four overloads cover the combinations: with and without message, on an ExtendedError base and on any other error class. With message, the returned class is a MessageErrorConstructornew Class({ context }), and still new Class(message, options) when a string comes first.

Each call returns a new class. Call it at module scope, once per class.

See Defining errors for worked examples of each shape.

function causeChain(error: unknown): unknown[]
function rootCause(error: unknown): unknown
function findCause<T>(
error: unknown,
predicate: (candidate: unknown) => candidate is T,
): T | undefined
function findCause(error: unknown, predicate: (candidate: unknown) => boolean): unknown
function findCauseOf<T>(
error: unknown,
constructor: abstract new (...args: never[]) => T,
): T | undefined
function hasCauseOf(
error: unknown,
constructor: abstract new (...args: never[]) => unknown,
): boolean

causeChain yields error first, stops at a primitive, and stops on a value already in the chain, so a cycle terminates. Everything else is built on it. See Cause chains.

function serializeError(
value: unknown,
options: SerializeErrorOptions & { includeOwnProperties: true },
): SerializedErrorWithProperties
function serializeError(value: unknown, options?: SerializeErrorOptions): SerializedError
OptionTypeDefaultEffect
includeStackbooleantrueInclude stack
maxDepthnumber8How far down the cause chain to walk
maxAggregatedErrorsnumber10AggregateError errors kept, across the whole output
includeOwnPropertiesbooleanfalseAlso copy the error’s own enumerable fields

Accepts any value. A non-error is returned as { name: typeof value, message: describeValue(value) }. context is copied through a JSON round trip. See Serialization.

function deserializeError(value: unknown, options?: DeserializeErrorOptions): Error
OptionTypeDefaultEffect
classesreadonly ErrorClass[][]Classes to rebuild by name, plus the built-ins
maxDepthnumber8How far down the chain to rebuild

A real Error is returned untouched; a value that is not error-shaped goes through toError. An unmatched name becomes an ExtendedError keeping that name.

function toError(value: unknown): Error
function isErrorLike(value: unknown): value is Error
function describeValue(value: unknown): string

All three are covered in Working with unknown values.