Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Improve error reporting when targetting the wrong Request class #54015

Closed
@nicolas-grekas

Description

@nicolas-grekas

When I create a controller action with a Request $request argument, the IDE will suggest me to use Symfony\Component\BrowserKit\Request first. If I'm not careful and select this, I'll get an error saying this:

Cannot autowire argument $request of "App\Controller[...]()": it references class "Symfony\Component\BrowserKit\Request" but no such service exists.

That's not good enough. Instead, it'd be better to tell something like "did you mean to use Symfony\Component\HttpFoundation\Request instead?". The same could happen with any other "Request" class of course.

The current error message comes from ServiceValueResolver trying to access an errored "request" service. This looks wrong also since this prevents any other resolver (lower priority ones) from resolving the argument. The error I would expect instead is the one thrown by ArgumentResolver itself when the resolver chain is over:

Controller "App\Controller[...]" requires that you provide a value for the "$request" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or there is a non-optional argument after this one.

My proposal to fix both issues would be to introduce a special kind of exceptions that ArgumentResolver would use to compute its error message if no other resolver resolved the argument.

Then, we would make RequestValueResolver throw this exception when a type ends with Request but has the wrong namespace for example.

WDYT?

Help wanted :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Good first issueIdeal for your first contribution! (some Symfony experience may be required)Help wantedIssues and PRs which are looking for volunteers to complete them.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions