Skip to content

[i18n] Send message codes with API messages (issues#3157) - #80

Merged
btopro merged 1 commit into
mainfrom
i18n/3157-message-codes
Oct 19, 2026
Merged

btopro merged 1 commit into
mainfrom
i18n/3157-message-codes

Conversation

@btopro

@btopro btopro commented Oct 19, 2026

Copy link
Copy Markdown
Member

Part of haxtheweb/issues#3157. Pairs with haxtheweb/webcomponents#883, which holds the source copy of the code list. Merge that first or alongside this. Nothing here depends on the editor: this only adds fields.

What it touches

  • Responses keep their English message (or error) and also get code + params, e.g.
    {status:404, data:{message:'Item not found for idOrSlug "intro"', code:'ITEM_NOT_FOUND', params:{idOrSlug:'intro'}}}
  • src/lib/haxcmsApiCodes.json: copy of @haxtheweb/haxcms-elements/lib/haxcms-api-codes.json (haxcms-php has the same file).
  • src/lib/apiMessageCodes.js:
    • Matches a message to its code and adds code/params at the top level, data, or __failed.
    • A code set by a handler is never overwritten.
    • 2xx responses only get codes for the five messages meant for success (e.g. "Site archived").
  • src/app.js: one middleware wraps res.json, so no route handlers changed.
  • src/cli.js: the CLI bridge annotates the same way, so in-process callers (the hax CLI) get identical bodies.
  • OpenAPI (site-spec, system-spec): document data.code and data.params on the error envelope.

Why recognise messages centrally instead of editing ~450 call sites

Coverage is complete today and the diff stays small. A route can still set code explicitly when it wants to. The trade-off is that new English messages need a pattern. The new test catches that: it parses src/ and fails on any message:/error: literal without a code.

Tests

  • test/unit/api-message-codes.test.cjs: catalog validity, examples, envelope shapes, middleware, the literal scan, and a sync check against the copy bundled in src/public/build (runs once a build with it lands).
  • app-server-upload-lifecycle expectations now include the code.
  • Full npm test locally: 2,666 pass, 0 fail. The pdf export tests need PUPPETEER_EXECUTABLE_PATH, same as before.
  • PHP parity: all 542 distinct messages from both backends give identical codes and params.

Generated by Claude Code

Responses keep their English `message` (or `error`) and now also carry a
stable `code` plus `params`, so the editor and the hax CLI can show the
message in the user's language.

- src/lib/haxcmsApiCodes.json: copy of the shared code list from
  @haxtheweb/haxcms-elements/lib/haxcms-api-codes.json (same file in
  haxcms-php). Each code has English text and patterns for the messages
  this backend already sends; named groups become params.
- src/lib/apiMessageCodes.js: matches a message to its code and adds
  code/params to the response body (top level, data, __failed). Codes a
  handler sets itself are kept. Successful responses only get codes for
  the few messages meant for success ("Site archived" and similar).
- app.js: middleware annotates every res.json; cli.js: the CLI bridge does
  the same so in-process callers get identical bodies.
- OpenAPI specs document data.code and data.params.
- Tests: matching, envelope shapes, middleware, and a scan that fails when
  a route sends a message literal with no code, so new messages get one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EN4mJgNh8uj2C44jDs4KAD
@codesandbox

codesandbox Bot commented Oct 19, 2026

Copy link
Copy Markdown

Review or Edit in CodeSandbox

Open the branch in Web Editor • VS Code • Insiders

Open Preview

Copilot AI balanced review requested due to automatic review settings October 19, 2026 17:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@btopro
btopro merged commit 9fdd99e into main Oct 19, 2026
6 checks passed
@btopro
btopro deleted the i18n/3157-message-codes branch October 19, 2026 17:54
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 19, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants