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

Skip to content

Commit ff3e504

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

File tree

3 files changed

+23
-19
lines changed

3 files changed

+23
-19
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/HttpKernel.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -154,18 +154,17 @@ private function handleRaw(Request $request, int $type = self::MASTER_REQUEST)
154154
$event = new GetResponseForControllerResultEvent($this, $request, $type, $response);
155155
$this->dispatcher->dispatch(KernelEvents::VIEW, $event);
156156

157-
if ($event->hasResponse()) {
158-
$response = $event->getResponse();
159-
} else {
160-
$msg = sprintf('The controller must return a "Symfony\Component\HttpFoundation\Response" object but it returned %s.', $this->varToString($response));
161-
162-
// the user may have forgotten to return something
163-
if (null === $response) {
164-
$msg .= ' Did you forget to add a return statement somewhere in your controller?';
165-
}
166-
167-
throw new ControllerDoesNotReturnResponseException($msg, $controller, __FILE__, __LINE__ - 17);
157+
if (!$event->hasResponse()) {
158+
$message = sprintf(
159+
'The controller must return a "Symfony\Component\HttpFoundation\Response" object but it returned %s.%s',
160+
$this->varToString($response),
161+
' Did you forget to add a return statement somewhere in your controller?'
162+
);
163+
164+
throw new ControllerDoesNotReturnResponseException($message, $controller, __FILE__, __LINE__ - 14);
168165
}
166+
167+
$response = $event->getResponse();
169168
}
170169

171170
return $this->filterResponse($response, $request, $type);

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