Skip to content

serializeError

serializeError(error, options): SerializedError

Serialize any thrown value into a plain object (SerializedError).

If the input has already been marked as serialized (via internal marker), it is returned directly. Otherwise, it traverses cause, AggregateError, and custom properties per options.

unknown

The value to serialize (Error, AggregateError, or any thrown).

ErrorSerialization = {}

Configuration for recursion, inclusion of cause/stack, etc.

SerializedError

The structured, plain-object representation of the error.

// Simple Error
const simple = new Error('oops');
console.log(serializeError(simple));
// Error with cause
const root = new Error('root');
const child = new Error('child', { cause: root });
console.log(serializeError(child, { cause: true }));
@example
// AggregateError example
const agg = new AggregateError([
new Error('a'),
new Error('b')
], 'multiple');
console.log(
serializeError(agg, {
aggregated: true,
stack: false
})
);