8000 [HttpKernel] Better exception page when the invokable controller retu… · symfony/symfony@f6c1622 · GitHub
[go: up one dir, main page]

Skip to content

Commit f6c1622

Browse files
committed
[HttpKernel] Better exception page when the invokable controller returns nothing
1 parent 91c5b14 commit f6c1622

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

src/Symfony/Component/HttpKernel/Exception/ControllerDoesNotReturnResponseException.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,15 @@ private function parseControllerDefinition(callable $controller): ?array
6767
if (\is_object($controller)) {
6868
$r = new \ReflectionClass($controller);
6969

70+
try {
71+
$line = $r->getMethod('__invoke')->getEndLine();
72+
} catch (\ReflectionException $e) {
73+
$line = $r->getEndLine();
74+
}
75+
7076
return [
7177
'file' => $r->getFileName(),
72-
'line' => $r->getEndLine(),
78+
'line' => $line,
7379
];
7480
}
7581

src/Symfony/Component/HttpKernel/Tests/HttpKernelTest.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -215,20 +215,19 @@ public function testHandleWhenTheControllerIsAStaticArray()
215215
public function testHandleWhenTheControllerDoesNotReturnAResponse()
216216
{
217217
$dispatcher = new EventDispatcher();
218-
$kernel = $this->getHttpKernel($dispatcher, function () { return 'foo'; });
218+
$kernel = $this->getHttpKernel($dispatcher, function () {});
219219

220220
try {
221221
$kernel->handle(new Request());
222222

223223
$this->fail('The kernel should throw an exception.');
224224
} catch (ControllerDoesNotReturnResponseException $e) {
225-
}
225+
$first = $e->getTrace()[0];
226226

227-
$first = $e->getTrace()[0];
228-
229-
// `file` index the array starting at 0, and __FILE__ starts at 1
230-
$line = file($first['file'])[$first['line'] - 2];
231-
$this->assertContains('// call controller', $line);
227+
// `file` index the array starting at 0, and __FILE__ starts at 1
228+
$line = file($first['file'])[$first['line'] - 2];
229+
$this->assertContains('// call controller', $line);
230+
}
232231
}
233232

234233
public function testHandleWhenTheControllerDoesNotReturnAResponseButAViewIsRegistered()

0 commit comments

Comments
 (0)
0