Skip to content

feat: add typed error factory - #1

Merged
DanhezCode merged 15 commits into
mainfrom
feat/error-factory
Sep 29, 2026
Merged

DanhezCode merged 15 commits into
mainfrom
feat/error-factory

Conversation

@DanhezCode

@DanhezCode DanhezCode commented Sep 22, 2026 •

Copy link
Copy Markdown
Member

Why

  • Error creation across the ecosystem needs a consistent, typed API that preserves standard Error behavior while supporting domain-specific metadata and stable codes.
  • Repeated error definitions should reuse defaults and fixed values without losing type inference.
  • The error system needs clear documentation and an appropriate skill so developers and coding agents can understand how to use it consistently.

What

  • Add BaseError with code, message, context, cause, and createdAt.
  • Add createError to generate typed factories with default and fixed configuration precedence.
  • Merge nested context values recursively so public and internal data can be layered without losing sibling fields.
  • Capture portable stack traces for generated errors.
  • Add and maintain documentation covering the error system and its supported usage patterns.
  • Add an appropriate skill that teaches developers and coding agents how to use the error system correctly.

Requirements

  • BaseError preserves standard Error semantics and exposes typed code, message, context, cause, and createdAt.
  • createError resolves values in the order defaults → per-error options → fixed.
  • Nested context entries merge recursively while preserving the exact inferred types.
  • Factory-created errors require a code when none is provided by defaults or fixed values.
  • The JSON representation includes only the public context without internal data.
  • All requirements and supported behavior must be covered by tests.
  • Documentation is up to date and covers the supported error APIs and usage patterns.
  • An appropriate skill is available and teaches how to use the error system correctly.

Constraints

  • No additional runtime dependency is introduced.

Related

N/A

Additional Notes

  • This is the first draft of the error system and is intentionally scoped to the shared typing and factory behavior needed by the rest of the ecosystem.

  • A runtime dependency on @opinionated-ts/primitives was introduced to reuse the shared merge implementation. This keeps the implementation reusable across packages and avoids bundling duplicate merge logic, while allowing the same primitive to be reused elsewhere in the ecosystem.

- define typed error code, message, and context
- preserve error causes and creation timestamps
- expose public context through JSON serialization
- keep internal context excluded from JSON output
- add createError factory with defaults and fixed options
- resolve error types from merged factory options
- recursively merge error context
- preserve factory stack traces
- use `@opinionated-ts/primitives` for object merging and type utilities
- Preserve literal and nested context types with `const` generics.
- Replace generic context mutation with typed recursive merges.
- Build only defined error options instead of spreading all sources.
- Reuse the merge type to expose the exact resolved option shape.
- Avoid separate merges for public and internal context.
- Skip context merging when no context is provided.
- Reduce intermediate object allocations during option resolution.
@DanhezCode
DanhezCode marked this pull request as ready for review September 29, 2026 00:38
@DanhezCode
DanhezCode merged commit 546dcf3 into main Sep 29, 2026
12 checks passed
@DanhezCode
DanhezCode deleted the feat/error-factory branch September 29, 2026 00:39
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 0.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant