Skip to content
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

Improve error reporting in router panel of web profiler #17744

Closed
wants to merge 9 commits into from

Conversation

javiereguiluz
Copy link
Member

Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #17342
License MIT
Doc PR -

Problem

If you define a route condition like this:

app:
    resource: '@AppBundle/Controller/'
    type:     annotation
    condition: "request.server.get('HTTP_HOST') matches '/.*\.dev/'"

When browsing the Routing panel in the web profiler, you see an exception:

problem

Why?

Because the route condition uses the request object, but the special TraceableUrlMatcher class doesn't get access to the real request object but to the special object obtained via:

$request = $profile->getCollector('request');

These are the contents of this pseudo-request:

cause

request.server.get(...) condition fails because request.server is null. The full exception message shows this:

exception

Solution

I propose to catch all exceptions in TraceableUrlMatcher and display an error message with some details of the exception:

error_message

@xabbuh
Copy link
Member

xabbuh commented Feb 9, 2016

Tests are falling now.

Status: Needs work

@javiereguiluz
Copy link
Member Author

I've fixed tests and I've added a test for the new error message.

@OskarStark
Copy link
Contributor

LGTM

@yceruto
Copy link
Member

yceruto commented Feb 24, 2016

@javiereguiluz I've investigated this problem too and I found a way to display the traces instead of display an exception message:

+use Symfony\Component\HttpFoundation\Request;

class TraceableUrlMatcher extends UrlMatcher
{
     //...
+   public function getTracesRequest(Request $request)
+   {
+       $this->request = $request;
+
+       $ret = $this->getTraces($request->getPathInfo());
+
+       $this->request = null;
+
+       return $ret;
+   }
}

And

use Symfony\Component\HttpFoundation\Request;

class RouterController
{
    //...
    public function panelAction($token)
    {
        //...

+       /** @var RequestDataCollector $request */
        $request = $profile->getCollector('request');

+       $traceRequest = Request::create(
+           $request->getPathInfo(),
+           $request->getRequestServer()->get('REQUEST_METHOD'),
+           $request->getRequestAttributes()->all(),
+           $request->getRequestCookies()->all(),
+           [],
+           $request->getRequestServer()->all()
+       );

        return new Response($this->twig->render('@WebProfiler/Router/panel.html.twig', array(
            'request' => $request,
            'router' => $profile->getCollector('router'),
-           'traces' => $matcher->getTraces($request->getPathInfo()),
+           'traces' => $matcher->getTracesRequest($traceRequest),
        )), 200, array('Content-Type' => 'text/html'));
    }
}

WDYT?

@javiereguiluz
Copy link
Member Author

I've made the changes suggested by @yceruto. Everything works as expected and his solution is much better than mine. Now you can see better error messages when a route with an expression doesn't match:

not_match

Thanks @yceruto!


$matchingRequest = Request::create('/foo', 'GET', array(), array(), array(), array('HTTP_USER_AGENT' => 'Firefox'));
$traces = $matcher->getTracesFromRequest($matchingRequest);
$this->assertEquals("Route matches!", $traces[0]['log']);
Copy link
Member

Choose a reason for hiding this comment

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

fabbot suggest to use single quote here.

@yceruto
Copy link
Member

yceruto commented Feb 29, 2016

AppVeyor build faild (no related)

@@ -40,6 +41,15 @@ public function getTraces($pathinfo)
return $this->traces;
}

public function getTracesFromRequest(Request $request)
Copy link
Member

Choose a reason for hiding this comment

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

getTraces*For*Request I think this is the pattern we are using elsewhere.

@fabpot
Copy link
Member

fabpot commented Feb 29, 2016

LGTM

@fabpot
Copy link
Member

fabpot commented Mar 3, 2016

Thank you @javiereguiluz.

fabpot added a commit that referenced this pull request Mar 3, 2016
…aviereguiluz)

This PR was squashed before being merged into the 2.7 branch (closes #17744).

Discussion
----------

Improve error reporting in router panel of web profiler

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #17342
| License       | MIT
| Doc PR        | -

### Problem

If you define a route condition like this:

```yaml
app:
    resource: '@AppBundle/Controller/'
    type:     annotation
    condition: "request.server.get('HTTP_HOST') matches '/.*\.dev/'"
```

When browsing the Routing panel in the web profiler, you see an exception:

![problem](https://cloud.githubusercontent.com/assets/73419/12930027/553eeb08-cf76-11e5-90b1-ab0de6175d4e.png)

#### Why?

Because the route condition uses the `request` object, but the special `TraceableUrlMatcher` class doesn't get access to the real `request` object but to the special object obtained via:

```php
$request = $profile->getCollector('request');
```

These are the contents of this pseudo-request:

![cause](https://cloud.githubusercontent.com/assets/73419/12930052/804ea248-cf76-11e5-9c38-2e43e1654065.png)

`request.server.get(...)` condition fails because `request.server` is `null`. The full exception message shows this:

![exception](https://cloud.githubusercontent.com/assets/73419/12930079/9c7d6058-cf76-11e5-8eeb-45f5059c824c.png)

### Solution

I propose to catch all exceptions in `TraceableUrlMatcher` and display an error message with some details of the exception:

![error_message](https://cloud.githubusercontent.com/assets/73419/12930106/b29e31d2-cf76-11e5-868c-98d8b0cc4e5b.png)

Commits
-------

1001554 Improve error reporting in router panel of web profiler
@fabpot fabpot closed this Mar 3, 2016
@stof
Copy link
Member

stof commented Mar 3, 2016

@javiereguiluz not passing a Request object when evaluating the condition should be considered as a bug IMO. Assuming that the collector has a compatible-enough API is not fine.

@stof
Copy link
Member

stof commented Mar 3, 2016

sorry, just saw that it was indeed done properly in the final diff. I commented too fast

@fabpot fabpot mentioned this pull request Mar 25, 2016
This was referenced Mar 27, 2016
@peshi
Copy link

peshi commented Mar 27, 2016

@javiereguiluz

Attempted to load class "Request" from namespace "Symfony\Bundle\WebProfilerBundle\Controller".

In vendor/symfony/symfony/src/Symfony/Bundle/WebProfilerBundle/Controller/RouterController.php
Line 86. $traceRequest = Request::create(

Missing "use" statement?

@xabbuh
Copy link
Member

xabbuh commented Mar 29, 2016

@peshi fixed in #18340

@fabpot fabpot mentioned this pull request Mar 30, 2016
ostrolucky pushed a commit to ostrolucky/symfony that referenced this pull request Mar 25, 2018
…iler (javiereguiluz)

This PR was squashed before being merged into the 2.7 branch (closes symfony#17744).

Discussion
----------

Improve error reporting in router panel of web profiler

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | symfony#17342
| License       | MIT
| Doc PR        | -

### Problem

If you define a route condition like this:

```yaml
app:
    resource: '@AppBundle/Controller/'
    type:     annotation
    condition: "request.server.get('HTTP_HOST') matches '/.*\.dev/'"
```

When browsing the Routing panel in the web profiler, you see an exception:

![problem](https://cloud.githubusercontent.com/assets/73419/12930027/553eeb08-cf76-11e5-90b1-ab0de6175d4e.png)

#### Why?

Because the route condition uses the `request` object, but the special `TraceableUrlMatcher` class doesn't get access to the real `request` object but to the special object obtained via:

```php
$request = $profile->getCollector('request');
```

These are the contents of this pseudo-request:

![cause](https://cloud.githubusercontent.com/assets/73419/12930052/804ea248-cf76-11e5-9c38-2e43e1654065.png)

`request.server.get(...)` condition fails because `request.server` is `null`. The full exception message shows this:

![exception](https://cloud.githubusercontent.com/assets/73419/12930079/9c7d6058-cf76-11e5-8eeb-45f5059c824c.png)

### Solution

I propose to catch all exceptions in `TraceableUrlMatcher` and display an error message with some details of the exception:

![error_message](https://cloud.githubusercontent.com/assets/73419/12930106/b29e31d2-cf76-11e5-868c-98d8b0cc4e5b.png)

Commits
-------

1001554 Improve error reporting in router panel of web profiler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants