Skip to content
Permalink
Browse files

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
-------

f6c1622 [HttpKernel] Better exception page when the invokable controller returns nothing
  • Loading branch information...
fabpot committed Mar 5, 2019
2 parents 0034e14 + f6c1622 commit c877cf8264c3acf8ce95226d9385c78a13f744fd
@@ -67,9 +67,15 @@ private function parseControllerDefinition(callable $controller): ?array
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,
];
}
@@ -215,20 +215,19 @@ public function testHandleWhenTheControllerIsAStaticArray()
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];
$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);
// `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()

0 comments on commit c877cf8

Please sign in to comment.
You can’t perform that action at this time.