[HttpKernel] Better exception page when the invokable controller returns nothing
This commit is contained in:
parent
91c5b14d8b
commit
f6c1622fb5
@ -67,9 +67,15 @@ class ControllerDoesNotReturnResponseException extends \LogicException
|
|||||||
if (\is_object($controller)) {
|
if (\is_object($controller)) {
|
||||||
$r = new \ReflectionClass($controller);
|
$r = new \ReflectionClass($controller);
|
||||||
|
|
||||||
|
try {
|
||||||
|
$line = $r->getMethod('__invoke')->getEndLine();
|
||||||
|
} catch (\ReflectionException $e) {
|
||||||
|
$line = $r->getEndLine();
|
||||||
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'file' => $r->getFileName(),
|
'file' => $r->getFileName(),
|
||||||
'line' => $r->getEndLine(),
|
'line' => $line,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -215,21 +215,20 @@ class HttpKernelTest extends TestCase
|
|||||||
public function testHandleWhenTheControllerDoesNotReturnAResponse()
|
public function testHandleWhenTheControllerDoesNotReturnAResponse()
|
||||||
{
|
{
|
||||||
$dispatcher = new EventDispatcher();
|
$dispatcher = new EventDispatcher();
|
||||||
$kernel = $this->getHttpKernel($dispatcher, function () { return 'foo'; });
|
$kernel = $this->getHttpKernel($dispatcher, function () {});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$kernel->handle(new Request());
|
$kernel->handle(new Request());
|
||||||
|
|
||||||
$this->fail('The kernel should throw an exception.');
|
$this->fail('The kernel should throw an exception.');
|
||||||
} catch (ControllerDoesNotReturnResponseException $e) {
|
} catch (ControllerDoesNotReturnResponseException $e) {
|
||||||
}
|
|
||||||
|
|
||||||
$first = $e->getTrace()[0];
|
$first = $e->getTrace()[0];
|
||||||
|
|
||||||
// `file` index the array starting at 0, and __FILE__ starts at 1
|
// `file` index the array starting at 0, and __FILE__ starts at 1
|
||||||
$line = file($first['file'])[$first['line'] - 2];
|
$line = file($first['file'])[$first['line'] - 2];
|
||||||
$this->assertContains('// call controller', $line);
|
$this->assertContains('// call controller', $line);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function testHandleWhenTheControllerDoesNotReturnAResponseButAViewIsRegistered()
|
public function testHandleWhenTheControllerDoesNotReturnAResponseButAViewIsRegistered()
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user