Skip to content

feat(error)!: require .withMessage() as terminal builder step (breaking)#94

Merged
braden-w merged 5 commits intomainfrom
braden-w/tagged-error-redesign
Feb 26, 2026
Merged

feat(error)!: require .withMessage() as terminal builder step (breaking)#94
braden-w merged 5 commits intomainfrom
braden-w/tagged-error-redesign

Conversation

@braden-w
Copy link
Collaborator

@braden-w braden-w commented Feb 26, 2026

The old createTaggedError API had a design flaw: every call site had to pass message as a string, even though most errors of the same type use the same message pattern. This led to duplicated message strings scattered across the codebase, and nothing stopped you from forgetting message entirely (TypeScript would catch it, but the intent was unclear). Worse, the builder returned factory functions and chain methods on the same object, blurring the line between "still configuring" and "ready to use."

This redesign introduces .withMessage(fn) as a required terminal step on the builder. It's the gate between configuration and usage — you don't get factory functions until you've defined how messages are computed.

createTaggedError('FileError')          ← ErrorBuilder (chain methods only)
  .withContext<{ path: string }>()      ← ErrorBuilder (chain methods only)
  .withCause<DbError | undefined>()    ← ErrorBuilder (chain methods only)
  .withMessage(({ context }) =>         ← FinalFactories (factory fns only)
    \`File op failed: \${context.path}\`)

The builder object has .withContext(), .withCause(), and .withMessage() — no factories. The object returned by .withMessage() has FileError() and FileErr() — no chain methods. You can't accidentally destructure factories from a half-configured builder.

Before / After

Before — message was a required field at every call site:

const { ApiError, ApiErr } = createTaggedError('ApiError')
  .withContext<{ endpoint: string }>();

ApiErr({
  message: 'Request failed',
  context: { endpoint: '/users' },
  cause: error,
});

After — message is defined once as a template, computed automatically:

const { ApiError, ApiErr } = createTaggedError('ApiError')
  .withContext<{ endpoint: string }>()
  .withCause()
  .withMessage(({ context }) => `Request to ${context.endpoint} failed`);

ApiErr({
  context: { endpoint: '/users' },
  cause: error,
});

The template function receives { name, context?, cause? } — whatever's available based on your builder configuration. You can still override the computed message at any call site by passing message: explicitly.

Context is now JsonObject

The context type constraint changed from Record<string, unknown> to JsonObject (new types: JsonValue, JsonObject). Error context must be JSON-serializable — no Date objects, no functions, no class instances. This guarantees errors survive serialization boundaries (logging, IPC, network transport) without silent data loss.

Other details

  • 50 tests rewritten covering the new API surface, including a test that verifies the builder object genuinely lacks factory methods
  • All 9 documentation files updated with the new pattern
  • Spec doc added: specs/20260225T000000-tagged-error-redesign.md

Verification

bun test passes. TypeScript compiles cleanly. The type-level enforcement was verified by checking that createTaggedError('Foo').FooError doesn't typecheck (factories don't exist on the builder).

- withMessage(fn) is now required and terminal — factories only available after calling it
- Factory input changes: message auto-computed from template, context/cause are the primary inputs, message is optional override
- Context constraint changed from Record<string, unknown> to JsonObject (JSON-serializable only)
- ErrorBuilder now has chain methods only (no factories); FinalFactories has factories only (no chain methods)
- Added JsonValue/JsonObject types, MessageFn/MessageInput types
- Rewrote 50 tests covering new API including builder-has-no-factories verification
- Updated README with new patterns, JsonObject constraint docs, Builder vs FinalFactories explainer
- Added spec doc: specs/20260225T000000-tagged-error-redesign.md
…age() terminal step

- Update core error-system.mdx: replace 'Flexible Mode' section with new .withMessage() pattern
- Update ERROR_HANDLING_GUIDE.md: replace rejected generator function section with createTaggedError recommendation
- Add .withMessage(fn) to all createTaggedError builders across 9 documentation files
- Remove 'message:' from factory call sites where message is now auto-computed from template
- Update context type display from Record<string, unknown> to JsonObject
- Add sections: Context/Cause constraints, JSON serializability, type extraction, updated Quick Reference

Files updated: error-system.mdx, ERROR_HANDLING_GUIDE.md, quick-start.mdx, svelte-tanstack.mdx,
tanstack-query.mdx, react-tanstack.mdx, error-transformation.mdx, real-world.mdx, service-layer.mdx
@braden-w braden-w changed the title docs: update all createTaggedError examples to use required .withMessage() terminal step feat(error)!: require .withMessage() as terminal builder step (breaking) Feb 26, 2026
… tests

Use definite assignment assertions for captured variables instead of
nullable types, and fix the IsJsonObject type test to use a concrete
JSON-safe type rather than testing JsonObject against itself.
@braden-w braden-w merged commit 7017df4 into main Feb 26, 2026
2 checks passed
@braden-w braden-w deleted the braden-w/tagged-error-redesign branch February 26, 2026 04:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant