Skip to content

fix: type plain onError return as error status response - #1966

Open
zacharywomack8-source wants to merge 1 commit into
elysiajs:mainfrom
zacharywomack8-source:fix/onError-response-type
Open

fix: type plain onError return as error status response#1966
zacharywomack8-source wants to merge 1 commit into
elysiajs:mainfrom
zacharywomack8-source:fix/onError-response-type

Conversation

@zacharywomack8-source

@zacharywomack8-source zacharywomack8-source commented Aug 16, 2026

Copy link
Copy Markdown

@algora-pbc /claim #313


Type-level fix for #313 (Option to return errors as JSON).

Problem
An onError handler that returns a plain value (e.g. { error: '...' } or a string) is currently typed as if the response is 200. At runtime the response keeps the status of the error that triggered the handler (400/404/422/500), so the types lie about what the client receives.

Change

  • Plain values returned from onError are now typed under every built-in error status (400, 404, 422, 500) instead of 200.
  • Explicit status(code, ...) / error(code, ...) returns still resolve to their exact status code via ExtractErrorFromHandle.
  • Applies to local, scoped, and global onError (single handler and array overloads), plus ErrorHandle composition in use/guard/macro.

Implementation

  • Adds ErrorValueToResponseSchema, ElysiaErrorHandlerToResponseSchema, ElysiaErrorHandlerToResponseSchemas, ElysiaErrorHandlerToResponseSchemaAmbiguous in src/types.ts.
  • Swaps the new types into the onError overloads and the ErrorHandle guards in src/index.ts.

Verification

  • Updated test/types/lifecycle/soundness.ts onError expectations: plain returns now produce the error-status union instead of 200.
  • pnpm test:types passes.

Summary by CodeRabbit

  • Improvements
    • Improved type inference for error-handler responses across local, scoped, and global error handling.
    • Error responses now accurately reflect supported status codes, including 400, 404, 422, and 500.
    • Enhanced type handling for single, multiple, and ambiguous error handlers.
  • Tests
    • Updated type-safety checks to validate the refined error response behavior.

claim-recheck-marker

@coderabbitai

coderabbitai Bot commented Aug 16, 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 Plus

Run ID: 70109022-727e-4c7b-b31a-248e30ded7a0

📥 Commits

Reviewing files that changed from the base of the PR and between 89088df and da62021.

📒 Files selected for processing (3)
  • src/index.ts
  • src/types.ts
  • test/types/lifecycle/soundness.ts

Included review availability: Your plan includes up to 10 reviews per rolling hour; 9 remain after this review.


Walkthrough

This change adds error-handler-specific response inference types. It applies them to onError, group, and guard declarations. Type-soundness expectations now cover statuses 400, 404, 422, and 500.

Changes

Error response inference

Layer / File(s) Summary
Error-handler response inference types
src/types.ts
Adds response schema utilities for single handlers, handler arrays, and ambiguous handlers. Non-undefined returns map to statuses 400, 404, 422, and 500.
Lifecycle type integration
src/index.ts
Applies the new utilities to global, scoped, and local onError, group, and guard declarations.
Type-soundness expectations
test/types/lifecycle/soundness.ts
Updates local, scoped, and global onError expectations. Removes the previous 200 response and adds the inferred error statuses.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Merge Risk: ⚪ Minimal · up to da620

This PR corrects the reported type mismatch for plain values returned by error handlers while preserving explicit status handling; no actionable merge-blocking risk remains after normal checks and review.

Possibly related PRs

  • elysiajs/elysia#1932: Introduces related error-handler response inference and maps plain onError returns to status 500.
  • elysiajs/elysia#1942: Extends the related inference with array and ambiguous-handler variants for onError, group, and guard.

Poem

Error types gather, statuses align ♡
Four hundred paths now clearly shine~
Arrays merge their schemas neat,
Guards and groups keep types complete.
No stray 200, silly thing—
Error responses wear their ring~ ( ̄▽ ̄)♡

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly identifies the main change: correcting plain onError return types to use error status responses.
Description check ✅ Passed The description explains the problem, implementation, affected APIs, related issue, and type-test verification in sufficient detail.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

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