Skip to content

Conversation

@SaltyAom
Copy link
Member

@SaltyAom SaltyAom commented Jan 5, 2026

Summary by CodeRabbit

  • Bug Fixes

    • Resolved ValueError exception handling where summary information was missing, improving error reporting consistency.
  • New Features

    • Introduced centralized error handler providing structured error responses with custom status codes (422 for validation errors, 404 for not-found).

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 5, 2026

Walkthrough

The PR adds version 1.4.22 to the changelog, refactors the example error handling code to use a centralized onError handler pattern with scoped configuration, and updates internal error type structures to support optional summary fields in a new ValueErrorWithSummary interface.

Changes

Cohort / File(s) Summary
Version Documentation
CHANGELOG.md
Added patch version 1.4.22 entry documenting a bug fix for ValueError related to missing summary types.
Example Error Handler
example/a.ts
Replaced exported app instance with errorHandler; restructured to use new Elysia({ name: "error-handler" }).onError(...).as("scoped") pattern with centralized error handling for VALIDATION (422) and NOT_FOUND (404) errors instead of WS/listen setup.
Error Type Structure
src/error.ts
Replaced MapValueError union type with new ValueErrorWithSummary interface extending ValueError with optional summary field; updated mapValueError() signature to accept non-optional ValueError and return ValueErrorWithSummary; updated ValidationError.all() return type and detail() method to safely compute summary only when valueError exists.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • patch 1.4.17 #1564: Modifies example/a.ts similarly to refactor the app export with onError and error-handler pattern.

Poem

🐰✨ Errors caught with grace and flair,
A summary here, a handler there,
New types dance where chaos once swirled,
Example and src now guard the world!

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'patch: 1.4.22' accurately reflects the main change—a version bump to 1.4.22 documented in CHANGELOG.md. While significant code changes exist in example/a.ts and src/error.ts, the PR's primary objective is the patch release, making the title appropriately focused and specific.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings

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.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Jan 5, 2026

Open in StackBlitz

npm i https://pkg.pr.new/elysiajs/elysia@1657

commit: 250dec6

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
example/a.ts (1)

25-39: Consider type narrowing for safer status code extraction.

The status code logic handles cases where error.status might be a string or invalid number. However, casting with Number() on non-numeric strings yields NaN, which then falls back to 500 via || 500. This works but could be clearer.

🔎 Slightly cleaner approach
 default: {
-	const statusCode =
-		"status" in error
-			? (typeof error.status === "number" ? error.status : Number(error.status)) || 500
-			: 500
+	let statusCode = 500
+	if ("status" in error && typeof error.status === "number") {
+		statusCode = error.status
+	}

 	let errMsg = "An error occurred"
📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 969fc76 and 250dec6.

📒 Files selected for processing (3)
  • CHANGELOG.md
  • example/a.ts
  • src/error.ts
🧰 Additional context used
🧬 Code graph analysis (1)
src/error.ts (1)
src/index.ts (2)
  • mapValueError (8224-8224)
  • error (3088-3128)
🔇 Additional comments (5)
CHANGELOG.md (1)

1-4: LGTM!

The changelog entry accurately documents the bug fix for missing summary types on ValueError, which corresponds to the new ValueErrorWithSummary interface introduced in src/error.ts.

src/error.ts (4)

47-49: LGTM!

Formatting change improves readability.


159-161: LGTM!

The new ValueErrorWithSummary interface cleanly extends ValueError with an optional summary field, addressing the type-safety issue mentioned in the changelog.


487-487: LGTM!

Return type correctly updated to ValueErrorWithSummary[] to reflect the mapped error structure.


599-601: LGTM!

Safe access pattern correctly guards against undefined valueError before calling mapValueError, avoiding potential runtime errors.

Comment on lines +6 to +17
case "VALIDATION": {
const allErrors = error.detail

return status(422, {
status: 422,
message: error.valueError?.message,
details: {
location: error.type,
rejected_value: error.value,
expected: error.expected,
},
})
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

error.detail is a method, not a property—and allErrors is unused.

detail() in ValidationError is a method requiring parameters (message, allowUnsafeValidatorDetails). Line 7 assigns the method reference itself (not its result) to allErrors, and the variable is never used.

Either remove the dead code or call the method properly:

🔎 Option 1: Remove unused code
 case "VALIDATION": {
-	const allErrors = error.detail
-
 	return status(422, {
🔎 Option 2: If you intended to get all errors
 case "VALIDATION": {
-	const allErrors = error.detail
+	const allErrors = error.all  // Use the getter for all validation errors

 	return status(422, {
 		status: 422,
 		message: error.valueError?.message,
 		details: {
 			location: error.type,
 			rejected_value: error.value,
 			expected: error.expected,
+			errors: allErrors,
 		},
 	})
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
case "VALIDATION": {
const allErrors = error.detail
return status(422, {
status: 422,
message: error.valueError?.message,
details: {
location: error.type,
rejected_value: error.value,
expected: error.expected,
},
})
case "VALIDATION": {
return status(422, {
status: 422,
message: error.valueError?.message,
details: {
location: error.type,
rejected_value: error.value,
expected: error.expected,
},
})
}
🤖 Prompt for AI Agents
In example/a.ts around lines 6 to 17, the code assigns error.detail (a method)
to allErrors and never uses it; either remove the unused assignment or call
detail(...) correctly and use its result. If you meant to discard it, delete the
const allErrors = error.detail line. If you meant to retrieve all validation
errors, call error.detail(message?, allowUnsafeValidatorDetails?) with
appropriate args, assign the returned value to a variable, and include that
value in the response (e.g., replace the current details payload or add an
errors field). Ensure the call matches the ValidationError.detail signature and
that the returned data is used instead of leaving a dead variable.

Comment on lines +163 to +166
export const mapValueError = (error: ValueError): ValueErrorWithSummary => {
if (!error) return error

let { message, path, value, type } = error
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Type signature inconsistency with runtime guard.

The parameter is typed as ValueError (non-nullable), but line 164 guards against falsy values and returns error (potentially undefined). This creates a mismatch: the return type is ValueErrorWithSummary, but the function can actually return undefined.

Consider either:

  1. Updating the signature to reflect nullable input/output: (error: ValueError | undefined): ValueErrorWithSummary | undefined
  2. Removing the guard if callers are guaranteed to pass non-null values
🔎 Option 1: Reflect nullable types
-export const mapValueError = (error: ValueError): ValueErrorWithSummary => {
-	if (!error) return error
+export const mapValueError = (error: ValueError | undefined): ValueErrorWithSummary | undefined => {
+	if (!error) return undefined
🤖 Prompt for AI Agents
In src/error.ts around lines 163 to 166, the function mapValueError is typed to
accept a non-nullable ValueError but contains a runtime guard that returns the
(possibly undefined) error, causing a signature mismatch; update the function
signature to accept and return nullable types like (error: ValueError |
undefined): ValueErrorWithSummary | undefined, or if callers never pass
undefined, remove the guard and keep the non-nullable signature—choose one
approach and adjust the type annotations accordingly so runtime behavior matches
the TypeScript types.

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.

2 participants