Skip to content

Commit 78b9ad7

Browse files
committed
feature #13780 [HttpKernel] Throw a LogicException when kernel.exception does not lead to a Response (nicolas-grekas)
This PR was merged into the 2.7 branch. Discussion ---------- [HttpKernel] Throw a LogicException when kernel.exception does not lead to a Response | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Commits ------- 22f4807 [HttpKernel] Throw a LogicException when kernel.exception does not led to a Response
2 parents 1bb834f + b932052 commit 78b9ad7

File tree

4 files changed

+17
-19
lines changed

4 files changed

+17
-19
lines changed

EventListener/ExceptionListener.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,15 @@ public static function getSubscribedEvents()
8383
*
8484
* @param \Exception $exception The \Exception instance
8585
* @param string $message The error message to log
86-
* @param bool $original False when the handling of the exception thrown another exception
8786
*/
88-
protected function logException(\Exception $exception, $message, $original = true)
87+
protected function logException(\Exception $exception, $message)
8988
{
90-
$isCritical = !$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500;
91-
$context = array('exception' => $exception);
9289
if (null !== $this->logger) {
93-
if ($isCritical) {
94-
$this->logger->critical($message, $context);
90+
if (!$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500) {
91+
$this->logger->critical($message, array('exception' => $exception));
9592
} else {
96-
$this->logger->error($message, $context);
93+
$this->logger->error($message, array('exception' => $exception));
9794
}
98-
} elseif (!$original || $isCritical) {
99-
error_log($message);
10095
}
10196
}
10297

HttpKernel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ private function handleException(\Exception $e, $request, $type)
229229
if (!$event->hasResponse()) {
230230
$this->finishRequest($request, $type);
231231

232-
throw $e;
232+
throw new \LogicException('No listeners of the "kernel.exception" event set a Response', 0, $e);
233233
}
234234

235235
$response = $event->getResponse();

Tests/DependencyInjection/ContainerAwareHttpKernelTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ public function testHandleRestoresThePreviousRequestOnException($type)
102102
$this->fail('->handle() suppresses the controller exception');
103103
} catch (\PHPUnit_Framework_Exception $exception) {
104104
throw $exception;
105-
} catch (\Exception $actual) {
106-
$this->assertSame($expected, $actual, '->handle() throws the controller exception');
105+
} catch (\LogicException $actual) {
106+
$this->assertSame($expected, $actual->getPrevious(), '->handle() throws the controller exception, wrapped when no listener');
107107
}
108108
}
109109

Tests/HttpKernelTest.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@
2323

2424
class HttpKernelTest extends \PHPUnit_Framework_TestCase
2525
{
26-
/**
27-
* @expectedException \RuntimeException
28-
*/
2926
public function testHandleWhenControllerThrowsAnExceptionAndRawIsTrue()
3027
{
31-
$kernel = new HttpKernel(new EventDispatcher(), $this->getResolver(function () { throw new \RuntimeException(); }));
32-
33-
$kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, true);
28+
$exception = new \RuntimeException();
29+
$kernel = new HttpKernel(new EventDispatcher(), $this->getResolver(function () use ($exception) { throw $exception; }));
30+
31+
try {
32+
$kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, true);
33+
$this->fail('LogicException expected');
34+
} catch (\LogicException $e) {
35+
$this->assertSame($exception, $e->getPrevious());
36+
}
3437
}
3538

3639
/**
@@ -132,7 +135,7 @@ public function testHandleWhenNoControllerIsFound()
132135
$dispatcher = new EventDispatcher();
133136
$kernel = new HttpKernel($dispatcher, $this->getResolver(false));
134137

135-
$kernel->handle(new Request());
138+
$kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, false);
136139
}
137140

138141
/**

0 commit comments

Comments
 (0)