Packing
Smuggle a full error through transports that only preserve message or cause.
Some transports — certain RPC layers, Cloudflare Durable Object stub boundaries, queues — only propagate an error’s message (or cause) and drop every custom property. Packing embeds the full serialized error into one of those surviving fields so it can be restored on the other side.
Packing
Section titled “Packing”import { EErrorPackType } from "@nice-code/error";
error.pack(EErrorPackType.msg_pack); // embeds the JSON into error.messageerror.pack(EErrorPackType.cause_pack); // embeds the JSON into error.causeThrow the packed error across the opaque boundary as you would any Error.
Unpacking
Section titled “Unpacking”error.unpack(); // restore the original NiceErrorIn practice you rarely call unpack by hand — castNiceError detects a packed payload and unpacks it automatically:
import { castNiceError } from "@nice-code/error";
const restored = castNiceError(caught); // unpacks if it was packedWhen to reach for it
Section titled “When to reach for it”- A Durable Object re-throws only
messageacross the stub call — pack withmsg_packbefore throwing inside the DO. - A job queue serializes only the error message — pack before enqueuing,
castNiceErrorafter dequeuing. - Any layer where you don’t control the serialization and only the standard
Errorfields survive.
For boundaries you do control (your own HTTP responses), prefer plain toJsonObject() + castNiceError — packing is specifically for the opaque ones.