fix: consistent error handling for custom validators in async validation contexts#6247
Merged
Eomm merged 14 commits intofastify:mainfrom Nov 8, 2025
Merged
Conversation
…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
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
Contributor
Author
|
Hello @Eomm Everything should be correct now. Thanks for your guidance! |
Eomm
approved these changes
Nov 1, 2025
Member
Eomm
left a comment
There was a problem hiding this comment.
I changed a bit the test to verify that sync and async stuff are in sync
jean-michelet
approved these changes
Nov 1, 2025
ilteoood
reviewed
Nov 3, 2025
Co-authored-by: Jean <110341611+jean-michelet@users.noreply.github.com> Signed-off-by: Manuel Spigolon <behemoth89@gmail.com>
ilteoood
approved these changes
Nov 8, 2025
Eomm
approved these changes
Nov 8, 2025
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
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
validateParamfunction inlib/validation.jsto wrap thevalidatorFunctioncall 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
validatorFunctioncall invalidateParamTesting
Breaking Changes
None. This change maintains backward compatibility while fixing the error handling inconsistency.
Fixes #6214