Skip to content

Commit aaa172a

Browse files
[HttpKernel] Create responses for unhandled HttpExceptionInterface exceptions
1 parent 4b66721 commit aaa172a

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

src/Symfony/Component/HttpKernel/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
4.1.0
55
-----
66

7+
* create `Reponse` objects for unhandled `HttpExceptionInterface` instead of throwing them
78
* added orphaned events support to `EventDataCollector`
89
* `ExceptionListener` now logs and collects exceptions at priority `2048` (previously logged at `-128` and collected at `0`)
910
* Deprecated `service:action` syntax with a single colon to reference controllers. Use `service::method` instead.

src/Symfony/Component/HttpKernel/HttpKernel.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,14 +221,17 @@ private function handleException(\Exception $e, Request $request, int $type): Re
221221
// a listener might have replaced the exception
222222
$e = $event->getException();
223223

224-
if (!$event->hasResponse()) {
224+
if ($event->hasResponse()) {
225+
$response = $event->getResponse();
226+
} elseif ($e instanceof HttpExceptionInterface) {
227+
$code = $e->getStatusCode();
228+
$response = new Response(isset(Response::$statusTexts[$code]) ? $code.' '.Response::$statusTexts[$code] : $code);
229+
} else {
225230
$this->finishRequest($request, $type);
226231

227232
throw $e;
228233
}
229234

230-
$response = $event->getResponse();
231-
232235
// the developer asked for a specific status code
233236
if (!$event->isAllowingCustomResponseCode() && !$response->isClientError() && !$response->isServerError() && !$response->isRedirect()) {
234237
// ensure that we actually have an error response

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,15 @@ public function testHandleWhenAListenerReturnsAResponse()
159159
$this->assertEquals('hello', $kernel->handle(new Request())->getContent());
160160
}
161161

162-
/**
163-
* @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
164-
*/
165162
public function testHandleWhenNoControllerIsFound()
166163
{
167164
$dispatcher = new EventDispatcher();
168165
$kernel = $this->getHttpKernel($dispatcher, false);
169166

170-
$kernel->handle(new Request());
167+
$response = $kernel->handle(new Request());
168+
169+
$this->assertSame(404, $response->getStatusCode());
170+
$this->assertSame('404 Not Found', $response->getContent());
171171
}
172172

173173
public function testHandleWhenTheControllerIsAClosure()

0 commit comments

Comments
 (0)