8000 [HttpKernel] Better exception page when the controller returns nothing · symfony/symfony@31a63b9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 31a63b9

Browse files
committed
[HttpKernel] Better exception page when the controller returns nothing
1 parent 6cb90da commit 31a63b9

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

src/Symfony/Component/HttpKernel/HttpKernel.php

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,29 @@ private function handleRaw(Request $request, int $type = self::MASTER_REQUEST)
162162
if (null === $response) {
163163
$msg .= ' Did you forget to add a return statement somewhere in your controller?';
164164
}
165-
throw new \LogicException($msg);
165+
166+
$e = new \LogicException($msg);
167+
168+
if (!$controllerDefinition = $this->parseControllerDefinition($controller)) {
169+
throw $e;
170+
}
171+
172+
$r = new \ReflectionProperty(\Exception::class, 'trace');
173+
$r->setAccessible(true);
174+
$r->setValue($e, array_merge(array(
175+
array(
176+
'line' => $e->getLine(),
177+
'file' => $e->getFile(),
178+
),
179+
), $e->getTrace()));
180+
$r = new \ReflectionProperty(\Exception::class, 'file');
181+
$r->setAccessible(true);
182+
$r->setValue($e, $controllerDefinition['file']);
183+
$r = new \ReflectionProperty(\Exception::class, 'line');
184+
$r->setAccessible(true);
185+
$r->setValue($e, $controllerDefinition['line']);
186+
187+
throw $e;
166188
}
167189
}
168190

@@ -281,4 +303,44 @@ private function varToString($var): string
281303

282304
return (string) $var;
283305
}
306+
307+
private function parseControllerDefinition(callable $controller): ?array
308+
{
309+
if (is_string($controller) && false !== strpos($controller, '::')) {
310+
$controller = explode('::', $controller);
311+
}
312+
313+
if (is_array($controller)) {
314+
try {
315+
$r = new \ReflectionMethod($controller[0], $controller[1]);
316+
317+
return array(
318+
'file' => $r->getFileName(),
319+
'line' => $r->getStartLine(),
320+
);
321+
} catch (\ReflectionException $e) {
322+
return null;
323+
}
324+
}
325+
326+
if ($controller instanceof \Closure) {
327+
$r = new \ReflectionFunction($controller);
328+
329+
return array(
330+
'file' => $r->getFileName(),
331+
'line' => $r->getStartLine(),
332+
);
333+
}
334+
335+
if (is_object($controller)) {
336+
$r = new \ReflectionClass($controller);
337+
338+
return array(
339+
'file' => $r->getFileName(),
340+
'line' => $r->getStartLine(),
341+
);
342+
}
343+
344+
return null;
345+
}
284346
}

0 commit comments

Comments
 (0)
0