Skip to content

fix: prefer registered error code#1920

Open
pupuking723 wants to merge 1 commit into
elysiajs:mainfrom
pupuking723:fix/registered-error-code-priority
Open

fix: prefer registered error code#1920
pupuking723 wants to merge 1 commit into
elysiajs:mainfrom
pupuking723:fix/registered-error-code-priority

Conversation

@pupuking723

@pupuking723 pupuking723 commented Jun 14, 2026

Copy link
Copy Markdown

Fixes #1919

When a custom error class defines its own code field, the runtime currently prefers that instance field over the key registered through .error(). That makes onError({ code }) receive a value that does not match the typed registered error key.

This changes both the AOT and dynamic error handlers to prefer the internal ERROR_CODE registration symbol, falling back to error.code for built-in errors and unregistered errors.

Verification:

  • npx -y bun test test/lifecycle/error.test.ts -t "uses registered error key over instance code"
  • npx -y bun test test/lifecycle/error.test.ts
  • npx -y bun run test:types
  • git diff --check

Summary by CodeRabbit

Bug Fixes

  • Corrected error handler precedence logic: Registered error handlers now consistently take priority when both instance codes and registered handlers exist. This ensures predictable error routing and response generation across validation errors, request handlers, and global error handlers.

@coderabbitai

coderabbitai Bot commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 62340572-6e82-4c71-8c17-3ac1acece631

📥 Commits

Reviewing files that changed from the base of the PR and between 56310be and 34624d6.

📒 Files selected for processing (3)
  • src/compose.ts
  • src/dynamic-handle.ts
  • test/lifecycle/error.test.ts

Walkthrough

Three files are changed to fix error code precedence. In src/compose.ts and src/dynamic-handle.ts, the resolution order is flipped so error[ERROR_CODE] (the registered key) takes priority over error.code (the instance property). A new parameterized test in test/lifecycle/error.test.ts covers this behavior for both AOT modes.

Changes

ERROR_CODE Precedence Fix

Layer / File(s) Summary
Error code precedence fix in AOT and dynamic handlers
src/dynamic-handle.ts, src/compose.ts
ERROR_CODE is imported in dynamic-handle.ts and createDynamicErrorHandler now uses error[ERROR_CODE] ?? error.code for errorContext.code. Both generated code paths in compose.ts (TypeBoxError branch and global error handler) are updated with the same flipped precedence.
Registered key precedence test
test/lifecycle/error.test.ts
Adds an it.each([true, false]) test that throws a custom error with code = 'invalid_url' registered under INVALID_URL, asserting the handler sees INVALID_URL and responds with status 422 and body 'handled'.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

めっ、バカじゃないの~♡
error.code なんて負けちゃったね~ (´∀`*)
ERROR_CODE がいちばんえらいんだよ~
ちゃんと直したんだから感謝してよねっ!
まあ… よくできました、かな~ ♡(。-`ω-)

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly describes the main change: reversing precedence to prefer the registered error code over instance properties.
Linked Issues check ✅ Passed All requirements from issue #1919 are met: error handlers now receive registered keys, not instance codes, across both AOT and dynamic modes with proper test coverage.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the error code precedence issue; no unrelated modifications detected.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install timed out. The project may have too many dependencies for the sandbox.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

onError code doesn't match the .error() key when the error class has its own code field

1 participant