Skip to content

[HttpKernel] Create responses for unhandled HttpExceptionInterface exceptions #26928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpKernel/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
4.1.0
-----

* create `Reponse` objects for unhandled `HttpExceptionInterface` instead of throwing them
* added orphaned events support to `EventDataCollector`
* `ExceptionListener` now logs and collects exceptions at priority `2048` (previously logged at `-128` and collected at `0`)
* Deprecated `service:action` syntax with a single colon to reference controllers. Use `service::method` instead.
Expand Down
9 changes: 6 additions & 3 deletions src/Symfony/Component/HttpKernel/HttpKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,17 @@ private function handleException(\Exception $e, Request $request, int $type): Re
// a listener might have replaced the exception
$e = $event->getException();

if (!$event->hasResponse()) {
if ($event->hasResponse()) {
$response = $event->getResponse();
} elseif ($e instanceof HttpExceptionInterface) {
$code = $e->getStatusCode();
$response = new Response(isset(Response::$statusTexts[$code]) ? $code.' '.Response::$statusTexts[$code] : $code);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to do this only in non-debug mode? And keep the exception in debug mode?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 for keeping them in debug

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind my prior comment. You already answered it in #26928 (comment). Please ignore. :-)

} else {
$this->finishRequest($request, $type);

throw $e;
}

$response = $event->getResponse();

// the developer asked for a specific status code
if (!$event->isAllowingCustomResponseCode() && !$response->isClientError() && !$response->isServerError() && !$response->isRedirect()) {
// ensure that we actually have an error response
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/HttpKernel/Tests/HttpKernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,15 @@ public function testHandleWhenAListenerReturnsAResponse()
$this->assertEquals('hello', $kernel->handle(new Request())->getContent());
}

/**
* @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function testHandleWhenNoControllerIsFound()
{
$dispatcher = new EventDispatcher();
$kernel = $this->getHttpKernel($dispatcher, false);

$kernel->handle(new Request());
$response = $kernel->handle(new Request());

$this->assertSame(404, $response->getStatusCode());
$this->assertSame('404 Not Found', $response->getContent());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As demonstrated by this test change, this changes the behavior.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, I would not do the change in 3.4.

}

public function testHandleWhenTheControllerIsAClosure()
Expand Down