Fix confusing crash when an unknown filter name is passed to optional/required - #2333
Open
hikmetba-bit wants to merge 1 commit into
Open
hikmetba-bit wants to merge 1 commit into
hikmetba-bit wants to merge 1 commit into
Conversation
…/required Fixes mojolicious#2129. Mojolicious::Validator::Validation::optional() mapped the list of filter names straight to callbacks (map { $self->validator-> filters->{$_} } @filters) before calling each one. A typo'd or unknown filter name silently produced an undef callback, and calling "$self->$cb(...)" on that undef then crashed with the confusing: Can't locate object method "" via package "Mojolicious::Validator::Validation" with no indication of which filter was actually missing. Now the loop walks the filter names directly and croaks with a clear "Unknown filter "<name>"" message as soon as it finds one with no matching entry in $self->validator->filters, before ever attempting the method call. Added a regression test in t/mojolicious/validation_lite_app.t. Verification note: this environment's Perl install is missing core modules (Pod::Usage, File::Spec internals) needed to load Mojolicious itself, so the real test suite could not be run here. The fix and its behavior (old code's exact crash message vs. new code's clear message, plus no regression for known filters and for zero filters) were verified with an isolated Perl port of just this loop's logic using only core Carp/Test::More, both of which do load correctly in this environment. Please double-check with the real test suite in CI/review before merge. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Member
|
Afraid we do not accept PRs with a |
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.
Summary
Fixes #2129.
Mojolicious::Validator::Validation::optional()(andrequired(), which calls it) mapped the list of filter names straight to callbacks before calling each one:If a filter name is unknown (typo, or the filter was never registered),
$self->validator->filters->{$_}isundef, and$self->$cb(...)then crashes with:— which gives no indication of which filter name was actually the problem, as the original reporter found while debugging a typo'd filter in production.
Fix
The loop now walks the filter names directly and croaks with a clear message naming the offending filter as soon as it finds one with no matching entry, before ever attempting the method call:
Carpwas already imported (use Carp ();) and this sameCarp::croakpattern is already used elsewhere in the same file (BUILD_DYNAMIC), so this stays consistent with the existing code style.Added a
Changesentry and a regression test int/mojolicious/validation_lite_app.t(Unknown filtersubtest) asserting the new, clear error message.Verification
This environment's Perl install could not load Mojolicious itself — core modules it depends on transitively (
Pod::Usage, and something inFile::Specresolution) are broken/missing in this particular install, so I could not run the real test suite (prove -l t/mojolicious/validation_lite_app.t) here.To still verify the actual logic change (not just eyeball it), I ported just the affected loop — old and new versions — into a standalone script using only
CarpandTest::More(both of which do load correctly here), and confirmed:Can't locate object method "" via package ...message on an unknown filter name.Unknown filter "<name>"message instead.All four checks passed. I could not run the actual
t/mojolicious/validation_lite_app.tfile I added the regression test to, or the wider suite, in this environment — please have CI/a reviewer confirm it passes before merge.Test plan
prove -l t/mojolicious/validation_lite_app.tpasses, including the new "Unknown filter" subtestmain, confirm this branch instead raises a clearUnknown filter "..."error🤖 Generated with Claude Code