Skip to content

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.

import { EErrorPackType } from "@nice-code/error";
error.pack(EErrorPackType.msg_pack); // embeds the JSON into error.message
error.pack(EErrorPackType.cause_pack); // embeds the JSON into error.cause

Throw the packed error across the opaque boundary as you would any Error.

error.unpack(); // restore the original NiceError

In 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 packed
  • A Durable Object re-throws only message across the stub call — pack with msg_pack before throwing inside the DO.
  • A job queue serializes only the error message — pack before enqueuing, castNiceError after dequeuing.
  • Any layer where you don’t control the serialization and only the standard Error fields survive.

For boundaries you do control (your own HTTP responses), prefer plain toJsonObject() + castNiceError — packing is specifically for the opaque ones.