Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[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

Merged
merged 1 commit into from
Jul 8, 2014

Conversation

weaverryan
Copy link
Member

...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):

some_route:
    pattern:  /
    defaults: { _controller: "Knp2UniversityBundle:Course:index" }

screen shot 2014-06-23 at 9 27 55 pm

screen shot 2014-06-23 at 9 48 14 pm

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!

@stof
Copy link
Member

stof commented Jun 24, 2014

👍

$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');
Copy link
Contributor

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

@fabpot
Copy link
Member

fabpot commented Jun 27, 2014

@weaverryan Can you finish this PR?

@wouterj
Copy link
Member

wouterj commented Jun 27, 2014

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).
@weaverryan
Copy link
Member Author

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!

@fabpot
Copy link
Member

fabpot commented Jul 8, 2014

👍

@fabpot
Copy link
Member

fabpot commented Jul 8, 2014

Thank you @weaverryan.

@fabpot fabpot merged commit f9b88c6 into symfony:master Jul 8, 2014
fabpot added a commit that referenced this pull request Jul 8, 2014
…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" }
```

![screen shot 2014-06-23 at 9 27 55 pm](https://cloud.githubusercontent.com/assets/121003/3367065/448e8298-fb54-11e3-92ea-9bf04510cb6d.png)

![screen shot 2014-06-23 at 9 48 14 pm](https://cloud.githubusercontent.com/assets/121003/3367063/3c79cf36-fb54-11e3-87c4-29428248ee47.png)

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
@weaverryan weaverryan deleted the better-bundle-exception branch October 17, 2014 17:53
fabpot added a commit that referenced this pull request May 17, 2016
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DX DX = Developer eXperience (anything that improves the experience of using Symfony) FrameworkBundle
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.