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

This commit is contained in:
Dmytro 2019-02-28 17:21:40 +02:00
parent 91c5b14d8b
commit f6c1622fb5
2 changed files with 13 additions and 8 deletions

View File

@ -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,
]; ];
} }

View File

@ -215,20 +215,19 @@ 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];
// `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() public function testHandleWhenTheControllerDoesNotReturnAResponseButAViewIsRegistered()