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'Exports
Section titled “Exports”| Export | Kind | What it does |
|---|---|---|
ExtendedError | class | Base class: name, code, context, cause, stack, toJSON() |
isExtendedError | function | value instanceof ExtendedError, as a type guard |
defineError | function | Returns a new error class |
causeChain | function | [error, error.cause, …], outermost first |
rootCause | function | The last value in the chain |
findCause | function | The first value in the chain a predicate accepts |
findCauseOf | function | The first instance of a class in the chain, typed as that class |
hasCauseOf | function | Whether the chain contains an instance of a class |
serializeError | function | A JSON-safe object for any thrown value |
deserializeError | function | Rebuilds serializeError output as instances of the original classes |
toError | function | An Error for any value; errors are returned unchanged |
isErrorLike | function | Whether a value is an object with a string message |
describeValue | function | A one-line string for any value |
The exported types are listed separately in Types.
ExtendedError
Section titled “ExtendedError”class ExtendedError<Context extends ErrorContext = ErrorContext> extends Error { static readonly code: string | undefined constructor(message: string, options?: ExtendedErrorOptions<Context>)}| Option | Type | Sets |
|---|---|---|
cause | unknown | error.cause, the native Error cause |
context | Context | error.context, typed by the class’s Context |
| Member | Value |
|---|---|
name | The name of the class that was constructed |
code | The constructed class’s static code, or undefined |
context | options.context, or undefined |
cause | options.cause. The property exists only when a cause was passed |
stack | Starts 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.
isExtendedError(value)
Section titled “isExtendedError(value)”function isExtendedError(value: unknown): value is ExtendedErrorinstanceof, 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.
defineError(name, options?)
Section titled “defineError(name, options?)”function defineError<Context extends ErrorContext = ErrorContext>( name: string, options?: DefineErrorOptions<Context>,): ExtendedErrorConstructor<Context>| Option | Type | Effect |
|---|---|---|
code | string | The class’s code. Omitted, it inherits the base’s |
base | an error class | The class to extend. Defaults to ExtendedError |
message | (context: Context) => string | Writes 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
MessageErrorConstructor — new 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.
Cause chain
Section titled “Cause chain”function causeChain(error: unknown): unknown[]function rootCause(error: unknown): unknownfunction findCause<T>( error: unknown, predicate: (candidate: unknown) => candidate is T,): T | undefinedfunction findCause(error: unknown, predicate: (candidate: unknown) => boolean): unknownfunction findCauseOf<T>( error: unknown, constructor: abstract new (...args: never[]) => T,): T | undefinedfunction hasCauseOf( error: unknown, constructor: abstract new (...args: never[]) => unknown,): booleancauseChain 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.
serializeError(value, options?)
Section titled “serializeError(value, options?)”function serializeError( value: unknown, options: SerializeErrorOptions & { includeOwnProperties: true },): SerializedErrorWithPropertiesfunction serializeError(value: unknown, options?: SerializeErrorOptions): SerializedError| Option | Type | Default | Effect |
|---|---|---|---|
includeStack | boolean | true | Include stack |
maxDepth | number | 8 | How far down the cause chain to walk |
maxAggregatedErrors | number | 10 | AggregateError errors kept, across the whole output |
includeOwnProperties | boolean | false | Also 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.
deserializeError(value, options?)
Section titled “deserializeError(value, options?)”function deserializeError(value: unknown, options?: DeserializeErrorOptions): Error| Option | Type | Default | Effect |
|---|---|---|---|
classes | readonly ErrorClass[] | [] | Classes to rebuild by name, plus the built-ins |
maxDepth | number | 8 | How 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.
toError(value)
Section titled “toError(value)”function toError(value: unknown): ErrorisErrorLike(value)
Section titled “isErrorLike(value)”function isErrorLike(value: unknown): value is ErrordescribeValue(value)
Section titled “describeValue(value)”function describeValue(value: unknown): stringAll three are covered in Working with unknown values.