Skip to content

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

Merged
Eomm merged 14 commits intofastify:mainfrom
emicovi:fix-async-validation-error-handling
Nov 8, 2025
Merged

fix: consistent error handling for custom validators in async validation contexts#6247
Eomm merged 14 commits intofastify:mainfrom
emicovi:fix-async-validation-error-handling

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!

@emicovi emicovi requested a review from Eomm September 15, 2025 07:56
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.

I changed a bit the test to verify that sync and async stuff are in sync

Copy link
Member

@gurgunday gurgunday left a comment

Choose a reason for hiding this comment

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

Agree with @ilteoood's comment

Eomm and others added 2 commits November 8, 2025 11:00
Co-authored-by: Jean <110341611+jean-michelet@users.noreply.github.com>
Signed-off-by: Manuel Spigolon <behemoth89@gmail.com>
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

Copy link
Member

@gurgunday gurgunday left a comment

Choose a reason for hiding this comment

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

lgtm

@Eomm Eomm merged commit d338dca into fastify:main Nov 8, 2025
30 checks passed
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

6 participants