-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[FrameworkBundle] Improving bad bundle exception in _controller #11210
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
Conversation
👍 |
$parser->parse('FooFakeBundle:Fake:index'); | ||
$this->fail('->parse() throws a \InvalidArgumentException if the bundle does not exist'); | ||
} catch (\Exception $e) { | ||
$this->assertInstanceOf('\InvalidArgumentException', $e, '->parse() throws a \InvalidArgumentException if the bundle does not exist'); |
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.
this does not actually test the new feature. You need to test the message
@weaverryan Can you finish this PR? |
I think this exception is caused by 2 common errors: (a) Forgetting to register the bundle or (b) a typo in the name. (b) is covered by the new exception, (a) was covered by the old exception. Isn't there a way we can do both? E.g. when there are no good alternatives found, give the suggestion about registering. Or always give the suggestion about registering and only add the alternatives suggestion when one is found. |
… controller in a route Usually, it is wrong because you've chosen the wrong bundle name in your _controller syntax. But this also tries to imply that you *might* be missing your bundle initialization in AppKernel (though I think this is much much less common).
I've just made all the suggested changes. @wouterj I altered the wording slightly to help with your situation (it was good thinking!). I do think that this situation is much less common, since devs usually generate a bundle (or are advanced enough to debug this easily if they create a bundle by hand) and so will always have the AppKernel entry. And this error will never happen with a 3rd party bundle, because the routing import will fail first. Thanks! |
👍 |
Thank you @weaverryan. |
…ontroller (weaverryan) This PR was merged into the 2.6-dev branch. Discussion ---------- [FrameworkBundle] Improving bad bundle exception in _controller ...ntroller in a routeHi guys! This improves the exception message when you use a bad bundle name in the `_controller` syntax in a routing file. Here is the before and after of the message with this mistake (real bundle is `KnpUniversityBundle`): ```yaml some_route: pattern: / defaults: { _controller: "Knp2UniversityBundle:Course:index" } ```   Notice the before and after behavior is the same `InvalidArgumentException` (just a different message). | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | Catching the plain `InvalidArgumentException` from `Kernel::getBundles()` seems a bit "loose". Should we consider creating a new exception (e.g. `BundleDoesNotExistException`) that extends `InvalidArgumentException` and throw it from inside `Kernel::getBundles`? This would allow us to catch more precisely, and it seems like it would be BC. Suggestions and thoughts warmly welcome! Thanks! Commits ------- f9b88c6 Improving the exception message when the bundle name is wrong for the controller in a route
This PR was merged into the 2.7 branch. Discussion ---------- Use levenshtein level for better Bundle matching | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | _I've targetted 2.7 branch since this was introduced in the 2.6 version but the 2.6 isn't maintain anymore._ **TL;DR:** I found unused code in bad bundle exception when Symfony try to find the best bundle to fix the typo. Should we remove that code (and got a potential lower matching bundle name) or keep it and make it work? ----- I've noticed that a part of the code wasn't used when determining which bundle typo was written on _bad bundle exception_, from #11210. ```php $alternative = null; $shortest = null; foreach ($bundleNames as $bundleName) { // if there's a partial match, return it immediately if (false !== strpos($bundleName, $nonExistentBundleName)) { return $bundleName; } $lev = levenshtein($nonExistentBundleName, $bundleName); if ($lev <= strlen($nonExistentBundleName) / 3 && ($alternative === null || $lev < $shortest)) { $alternative = $bundleName; } } ``` In this snippet, the `$shortest` wasn't update in the `foreach`. Reading the code, I guess it was supposed to add an even better accuracy when multiple bundle matche the typo'd bundle name. Which mean when an alternative is found, we have to assign the level `$lev` from that match to `$shortest`. ```php if ($lev <= strlen($nonExistentBundleName) / 3 && ($alternative === null || $lev < $shortest)) { $alternative = $bundleName; $shortest = $lev; } ``` Let say you have these bundles: `FoooooBundle` and `FooBundle` and you request the bundle `FoodBundle`. - Without `$shortest` updated, you'll got a suggestion with `FoooooBundle` (first matching bundle found) - With `$shortest` upadted, you'll got a suggestion with `FooBundle` (because it has a better level than `FoooooBundle`) This isn't a _bug fix_ since this is only supposed to help developper but not the final user. **Question is**: should we keep that level comparison or just remove it? Commits ------- ac7f74e Use levenshtein level for better Bundle matching
...ntroller in a routeHi guys!
This improves the exception message when you use a bad bundle name in the
_controller
syntax in a routing file. Here is the before and after of the message with this mistake (real bundle isKnpUniversityBundle
):Notice the before and after behavior is the same
InvalidArgumentException
(just a different message).Catching the plain
InvalidArgumentException
fromKernel::getBundles()
seems a bit "loose". Should we consider creating a new exception (e.g.BundleDoesNotExistException
) that extendsInvalidArgumentException
and throw it from insideKernel::getBundles
? This would allow us to catch more precisely, and it seems like it would be BC.Suggestions and thoughts warmly welcome!
Thanks!