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

Skip to content

Commit c8f75e5

Browse files
committed
[HttpKernel] Better exception page when the controller returns nothing
1 parent 47da23
10000
c commit c8f75e5

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\Exception;
13+
14+
/**
15+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
16+
*/
17+
class ControllerDoesNotReturnResponseException extends \LogicException
18+
{
19+
public function __construct(string $message, callable $controller, string $file, int $line)
20+
{
21+
parent::__construct($message);
22+
23+
if (!$controllerDefinition = $this->parseControllerDefinition($controller)) {
24+
return;
25+
}
26+
27+
$this->file = $controllerDefinition['file'];
28+
$this->line = $controllerDefinition['line'];
29+
$r = new \ReflectionProperty(\Exception::class, 'trace');
30+
$r->setAccessible(true);
31+
$r->setValue($this, array_merge(array(
32+
array(
33+
'line' => $line,
34+
'file' => $file,
35+
),
36+
), $this->getTrace()));
37+
}
38+
39+
private function parseControllerDefinition(callable $controller): ?array
40+
{
41+
if (is_string($controller) && false !== strpos($controller, '::')) {
42+
$controller = explode('::', $controller);
43+
}
44+
45+
if (is_array($controller)) {
46+
try {
47+
$r = new \ReflectionMethod($controller[0], $controller[1]);
48+
49+
return array(
50+
'file' => $r->getFileName(),
51+
'line' => $r->getEndLine(),
52+
);
53+
} catch (\ReflectionException $e) {
54+
return null;
55+
}
56+
}
57+
58+
if ($controller instanceof \Closure) {
59+
$r = new \ReflectionFunction($controller);
60+
61+
return array(
62+
'file' => $r->getFileName(),
63+
'line' => $r->getEndLine(),
64+
);
65+
}
66+
67+
if (is_object($controller)) {
68+
$r = new \ReflectionClass($controller);
69+
70+
return array(
71+
'file' => $r->getFileName(),
72+
'line' => $r->getEndLine(),
73+
);
74+
}
75+
76+
return null;
77+
}
78+
}

src/Symfony/Component/HttpKernel/HttpKernel.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
1717
use Symfony\Component\HttpKernel\Event\FilterControllerArgumentsEvent;
1818
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
19+
use Symfony\Component\HttpKernel\Exception\ControllerDoesNotReturnResponseException;
1920
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2021
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
2122
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
@@ -162,7 +163,8 @@ private function handleRaw(Request $request, int $type = self::MASTER_REQUEST)
162163
if (null === $response) {
163164
$msg .= ' Did you forget to add a return statement somewhere in your controller?';
164165
}
165-
throw new \LogicException($msg);
166+
167+
throw new ControllerDoesNotReturnResponseException($msg, $controller, __FILE__, __LINE__ - 17);
166168
}
167169
}
168170

0 commit comments

Comments
 (0)
0