feature #30408 [HttpKernel] Better exception page when the invokable controller returns nothing (dimabory)

This PR was merged into the 4.3-dev branch.

Discussion
----------

[HttpKernel] Better exception page when the invokable controller returns nothing

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

---

__Prerequisites__
_Configure invokable controller_
```php
# config/routes.yaml
index:
    path: /
    controller: App\Controller\Start
```

__Before:__
![before](https://user-images.githubusercontent.com/11414342/53577698-ca739000-3b7e-11e9-98ac-8c8e27626fbe.png)

__After:__
![after](https://user-images.githubusercontent.com/11414342/53577733-df502380-3b7e-11e9-8377-a4a97ea73df8.png)

---

Take a look for an enhancement/refactoring in `HttpKernel.php`

Commits
-------

f6c1622fb5 [HttpKernel] Better exception page when the invokable controller returns nothing
This commit is contained in:
Fabien Potencier 2019-03-05 10:44:46 +01:00
commit c877cf8264
2 changed files with 13 additions and 8 deletions

View File

@ -67,9 +67,15 @@ class ControllerDoesNotReturnResponseException extends \LogicException
if (\is_object($controller)) {
$r = new \ReflectionClass($controller);
try {
$line = $r->getMethod('__invoke')->getEndLine();
} catch (\ReflectionException $e) {
$line = $r->getEndLine();
}
return [
'file' => $r->getFileName(),
'line' => $r->getEndLine(),
'line' => $line,
];
}

View File

@ -215,20 +215,19 @@ class HttpKernelTest extends TestCase
public function testHandleWhenTheControllerDoesNotReturnAResponse()
{
$dispatcher = new EventDispatcher();
$kernel = $this->getHttpKernel($dispatcher, function () { return 'foo'; });
$kernel = $this->getHttpKernel($dispatcher, function () {});
try {
$kernel->handle(new Request());
$this->fail('The kernel should throw an exception.');
} catch (ControllerDoesNotReturnResponseException $e) {
$first = $e->getTrace()[0];
// `file` index the array starting at 0, and __FILE__ starts at 1
$line = file($first['file'])[$first['line'] - 2];
$this->assertContains('// call controller', $line);
}
$first = $e->getTrace()[0];
// `file` index the array starting at 0, and __FILE__ starts at 1
$line = file($first['file'])[$first['line'] - 2];
$this->assertContains('// call controller', $line);
}
public function testHandleWhenTheControllerDoesNotReturnAResponseButAViewIsRegistered()