Skip to content

Commit 1e1d73e

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

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/Symfony/Component/HttpKernel/HttpKernel.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -230,14 +230,16 @@ 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+
$response = new Response($e->getMessage());
237+
} else {
234238
$this->finishRequest($request, $type);
235239

236240
throw $e;
237241
}
238242

239-
$response = $event->getResponse();
240-
241243
// the developer asked for a specific status code
242244
if ($response->headers->has('X-Status-Code')) {
243245
@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

+4-4
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('Unable to find the controller for path "/". The route is wrongly configured.', $response->getContent());
189189
}
190190

191191
public function testHandleWhenTheControllerIsAClosure()

0 commit comments

Comments
 (0)