Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ca4acae

Browse files
[HttpKernel] Create responses for unhandled HttpExceptionInterface exceptions
1 parent 811c4dd commit ca4acae

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/Symfony/Component/HttpKernel/HttpKernel.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,14 +230,17 @@ private function handleException(\Exception $e, $request, $type)
230230
// a listener might have replaced the exception
231231
$e = $event->getException();
232232

233-
if (!$event->hasResponse()) {
233+
if ($event->hasResponse()) {
234+
$response = $event->getResponse();
235+
} elseif ($e instanceof HttpExceptionInterface) {
236+
$code = $e->getStatusCode();
237+
$response = new Response(isset(Response::$statusTexts[$code]) ? $code.' '.Response::$statusTexts[$code] : $code);
238+
} else {
234239
$this->finishRequest($request, $type);
235240

236241
throw $e;
237242
}
238243

239-
$response = $event->getResponse();
240-
241244
// the developer asked for a specific status code
242245
if ($response->headers->has('X-Status-Code')) {
243246
@trigger_error(sprintf('Using the X-Status-Code header is deprecated since Symfony 3.3 and will be removed in 4.0. Use %s::allowCustomResponseCode() instead.', GetResponseForExceptionEvent::class), E_USER_DEPRECATED);

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

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

180-
/**
181-
* @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
182-
*/
183180
public function testHandleWhenNoControllerIsFound()
184181
{
185182
$dispatcher = new EventDispatcher();
186183
$kernel = $this->getHttpKernel($dispatcher, false);
187184

188-
$kernel->handle(new Request());
185+
$response = $kernel->handle(new Request());
186+
187+
$this->assertSame(404, $response->getStatusCode());
188+
$this->assertSame('404 Not Found', $response->getContent());
189189
}
190190

191191
public function testHandleWhenTheControllerIsAClosure()

0 commit comments

Comments
 (0)