Skip to content

[P3] Preserve richer Error details such as stack and custom fields #74

Description

@samlaycock

Priority

P3

Area

Feature enhancement: richer built-in type fidelity

Problem

The built-in Error handler preserves name, message, and cause, but drops other useful diagnostic state such as stack and enumerable custom fields.

Example fields that are currently lost:

const err = new Error("failed");
err.stack; // present in most runtimes
(err as Error & { code?: string }).code = "E_TEST";

const result = decode<{ err: Error }>(encode({ err })).err;
result.stack; // new local stack or undefined, not the original stack
(result as Error & { code?: string }).code; // undefined

Relevant code:

  • src/types.ts: serializeErrorDetails() only emits name, message, and optional cause.
  • src/types.ts: deserializeErrorDetails() reconstructs a plain Error and assigns name.

Expected behavior

Consider an opt-in or backwards-compatible enhancement that preserves more diagnostic details for callers who use Error values as transportable diagnostics.

Suggested approach

Keep the existing structured marker for compatibility, and add optional fields such as stack and a JSON-safe record of enumerable own properties. Avoid executing constructors for arbitrary error names.

Acceptance criteria

  • Add tests for preserving stack when present.
  • Add tests for JSON-compatible enumerable custom fields such as code or status.
  • Existing legacy Error message strings and current structured Error payloads still decode correctly.
  • Document which Error fields are preserved and which are intentionally excluded.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions