Edition 0.1.1
Chart of safe passage for thrown errors
Surveyed for Node.js, Bun, Deno and browsers
Send errors across processes and bring them back whole.
faultferry turns any thrown value into plain JSON and rebuilds it on the other side: the same class, message, stack, cause chain, AggregateError members and custom properties. It works across workers, child processes, HTTP and message queues, and it never throws on hostile input.
- 4.7 kBgzip, zero dependencies
- 126tests, 100% line coverage
- 1.7xfaster than serialize-error on every case measured
01Sailing directions
Send an error from one process to another in three steps
Install the package on both sides.
$ npm i faultferryWhere the error is caught, serialize it before it crosses the boundary.
import { serialize } from 'faultferry'; try { await job(); } catch (error) { channel.send(JSON.stringify(serialize(error))); }Where it arrives, deserialize it and throw or log it as the original error.
import { deserialize } from 'faultferry'; channel.on('message', (text) => { throw deserialize(JSON.parse(text)); });
02Notice to mariners
Errors do not survive the trip
JSON.stringify(new Error('boom')) gives you an empty object. Workers, IPC and HTTP all move data, not errors, so the class, the cause and every custom field are lost on the way.
JSON drops everything
message and stack are not enumerable, so they vanish. Error classes, causes and AggregateError members vanish with them.
structuredClone is not enough
It keeps a cause, but custom classes come back as plain Error, AggregateError loses its errors and own properties like status or code are dropped.
Hand rolled code breaks
A cycle in a cause chain, a getter that throws or a revoked proxy turns a simple error logger into a crash in the code that was supposed to report the crash.
const cause = new RangeError('root');
JSON.stringify(new TypeError('boom', { cause }));
// '{}'
JSON.stringify(serialize(new TypeError('boom', { cause })));
// {"$error":"TypeError","name":"TypeError",
// "message":"boom","stack":"...",
// "cause":{"$error":"RangeError","name":"RangeError",
// "message":"root","stack":"..."}}03Logbook
Build an error, ferry it, inspect what lands
Everything here runs the real library in your browser. Shape an error, read the JSON that would cross the wire, then edit that JSON and watch deserialize rebuild it.
04Chart legend
Everything an error carries, nothing it cannot
- Exact round trips
- Prototype, name, message, stack and every own enumerable property come back, with the same enumerability as the original.
- Cause chains with cycles
- Cycles and shared references travel as $ref pointers and are rebuilt with the same identity, instead of becoming the string [Circular].
- Every built in error
- AggregateError.errors, SuppressedError.error and suppressed, and DOMException name and code, from this realm or another one.
- Custom classes
- register(HttpError) on both sides and instances come back as HttpError. Constructors are never called, so required arguments do not matter.
- Any thrown value
- Strings, objects, undefined, NaN, -0, BigInt, Date, RegExp, Map and Set all survive. Nothing is wrapped in a fake error.
- Hostile input is fine
- Getters that throw, revoked proxies, prototype traps, huge stacks and garbage JSON: serialize and deserialize never throw.
- Limits you control
- maxDepth, maxNodes, maxLength and maxItems keep payloads bounded. stack: false strips traces before errors reach a client.
- isError that tells the truth
- Uses the native Error.isError when present and a correct fallback otherwise, including errors from iframes and node:vm contexts.
05Routes
Drop it in where errors cross a boundary
06Pilot book
Five functions
serialize(value, options?)- Returns a JSON safe value. Options: maxDepth (32), maxNodes (10000), maxLength, maxItems, stack (true), toJSON (true), classes.
deserialize(data, options?)- Rebuilds errors with the right prototype. Unknown classes become Error with the original name. Options: maxDepth (256), classes.
register(ErrorClass, name?)- Registers a class under a wire name (default: the class name). Returns the class.
unregister(classOrName)- Removes a registered class by class or by name.
isError(value)- True for real error objects, cross realm safe.
07Tide table
How it compares
Measured on Node 24 with structuredClone and serialize-error 13.1.0. Each cell is what you get back after a round trip.
| After a round trip | faultferry | serialize-error 13.1.0 | structuredClone |
|---|---|---|---|
| Custom class instance | Kept | Needs addKnownErrorConstructor | Lost |
| AggregateError members | Kept | Kept | Lost |
| SuppressedError | Kept | Lost | Lost |
| DOMException | Kept | Plain Error with name and code | Kept |
| Circular cause | Kept | Becomes the string [Circular] | Kept |
| Own properties (code, errno, status) | Kept | Kept | Lost |
| undefined, NaN, BigInt, Date values | Kept | Dropped or turned into strings | Kept |
| Thrown string | Kept | Wrapped in NonError | Kept |
| Getters that throw | Skipped, never throws | Throws on an own property getter | Throws on a message getter |
| JSON text output | Kept | Kept | Lost |
- Key
- kept
- lost
- partly kept
08Speed trials
Fast on both ends
Operations per second, median of five runs, higher is better.
serialize + JSON.stringify
- faultferry
- serialize-error
- structuredClone (no JSON)
JSON.parse + deserialize
- faultferry
- serialize-error
09Draught
Small enough to forget about
One ESM file with a CommonJS build, typed with a hand written index.d.ts, tree shakeable and free of side effects.
Runs where your errors run
- Node.js 18 and newer
- Bun
- Deno
- Modern browsers
- Web Workers and Service Workers
- ESM, CommonJS, TypeScript
10Harbor master
Questions
Ship your next error across the river.
One install, five functions, zero dependencies. Errors leave with their class, cause and fields, and arrive with all of them.
$ npm i faultferry