Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1bb997b

Browse filesBrowse files
[HttpKernel] Create responses for unhandled HttpExceptionInterface exceptions
1 parent 4b66721 commit 1bb997b
Copy full SHA for 1bb997b

File tree

2 files changed

+10
-7
lines changed
Filter options

2 files changed

+10
-7
lines changed

‎src/Symfony/Component/HttpKernel/HttpKernel.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/HttpKernel.php
+6-3Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Tests/HttpKernelTest.php
+4-4Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.