feat(error)!: require .withMessage() as terminal builder step (breaking)#94
Merged
feat(error)!: require .withMessage() as terminal builder step (breaking)#94
Conversation
- 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
… 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The old
createTaggedErrorAPI had a design flaw: every call site had to passmessageas 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 forgettingmessageentirely (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.The builder object has
.withContext(),.withCause(), and.withMessage()— no factories. The object returned by.withMessage()hasFileError()andFileErr()— 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:
After — message is defined once as a template, computed automatically:
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 passingmessage:explicitly.Context is now
JsonObjectThe
contexttype constraint changed fromRecord<string, unknown>toJsonObject(new types:JsonValue,JsonObject). Error context must be JSON-serializable — noDateobjects, no functions, no class instances. This guarantees errors survive serialization boundaries (logging, IPC, network transport) without silent data loss.Other details
specs/20260225T000000-tagged-error-redesign.mdVerification
bun testpasses. TypeScript compiles cleanly. The type-level enforcement was verified by checking thatcreateTaggedError('Foo').FooErrordoesn't typecheck (factories don't exist on the builder).