-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
emicovi
wants to merge
6
commits into
fastify:main
Choose a base branch
from
emicovi:fix-async-validation-error-handling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…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
Eomm
reviewed
Jul 31, 2025
There was a problem hiding this 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
reviewed
Aug 1, 2025
- 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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
Solution
Modified the
validateParam
function inlib/validation.js
to wrap thevalidatorFunction
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
validatorFunction
call invalidateParam
Testing
Breaking Changes
None. This change maintains backward compatibility while fixing the error handling inconsistency.
Fixes #6214