Skip to content

fix: consistent error handling for custom validators in async validation contexts #6247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

emicovi
Copy link
Contributor

@emicovi emicovi commented Jul 4, 2025

Description

Fixes inconsistent error handling between sync and async validation contexts when custom validators throw errors instead of returning {error} objects.

Problem

When using async preValidation hooks with custom validators, if the validator throws an error (instead of returning {error}), the error would not be caught by the error handler and would result in an unhandled promise rejection, potentially crashing the application.

This inconsistency meant:

  • Sync context: Thrown errors were caught and handled properly by the error handler
  • Async context: Thrown errors became unhandled promise rejections

Solution

Modified the validateParam function in lib/validation.js to wrap the validatorFunction call in a try-catch block. This ensures that synchronous throws are caught and returned as validation errors, making error handling consistent across both sync and async validation contexts.

Changes

  • lib/validation.js: Added try-catch block around validatorFunction call in validateParam
  • test/validation-error-handling.test.js: Added test case to verify error handling consistency

Testing

Breaking Changes

None. This change maintains backward compatibility while fixing the error handling inconsistency.

Fixes #6214

emicovi and others added 2 commits July 4, 2025 13:57
…ion contexts

- Wrap validatorFunction call in try-catch to handle synchronous throws
- Ensures thrown errors are caught consistently in both sync and async validation contexts
- Fixes issue where custom validators throwing errors in async preValidation hooks
  would cause unhandled promise rejections instead of triggering error handlers
- Add test case to verify error handling consistency

Fixes fastify#6214
Copy link
Member

@Eomm Eomm left a comment

Choose a reason for hiding this comment

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

minor suggestions

nice work!

@Eomm Eomm added the bugfix Issue or PR that should land as semver patch label Jul 31, 2025
@emicovi emicovi requested a review from Eomm July 31, 2025 13:10
emicovi added 2 commits August 6, 2025 15:53
- Add err.statusCode = 500 inside catch block of validateParam()
- Update test expectation from 500 to 200 status code
- Ensures thrown errors are properly handled as internal errors
- Change response status code from 200 to 500 when custom validation fails
- Ensure error messages are sent correctly in the response
@emicovi
Copy link
Contributor Author

emicovi commented Aug 6, 2025

Hello @Eomm Everything should be correct now. Thanks for your guidance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix Issue or PR that should land as semver patch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

async preValidation with custom validator does not trigger error handler
3 participants