Skip to content

[HttpKernel] Provide status code in fragment handler exception #37537

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

Merged
merged 1 commit into from
Aug 10, 2020
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpKernel\Controller\ControllerReference;
use Symfony\Component\HttpKernel\Exception\HttpException;

/**
* Renders a URI that represents a resource fragment.
Expand Down Expand Up @@ -97,7 +98,8 @@ public function render($uri, string $renderer = 'inline', array $options = [])
protected function deliver(Response $response)
{
if (!$response->isSuccessful()) {
throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %d).', $this->requestStack->getCurrentRequest()->getUri(), $response->getStatusCode()));
$responseStatusCode = $response->getStatusCode();
throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %d).', $this->requestStack->getCurrentRequest()->getUri(), $responseStatusCode), 0, new HttpException($responseStatusCode));
}

if (!$response instanceof StreamedResponse) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Fragment\FragmentHandler;

/**
Expand Down Expand Up @@ -53,11 +54,20 @@ public function testRenderWithUnknownRenderer()

public function testDeliverWithUnsuccessfulResponse()
{
$this->expectException('RuntimeException');
$this->expectExceptionMessage('Error when rendering "http://localhost/" (Status code is 404).');
$handler = $this->getHandler($this->returnValue(new Response('foo', 404)));

$handler->render('/', 'foo');
try {
$handler->render('/', 'foo');
$this->fail('->render() throws a \RuntimeException exception if response is not successful');
} catch (\Exception $e) {
$this->assertInstanceOf('\RuntimeException', $e);
$this->assertEquals(0, $e->getCode());
$this->assertEquals('Error when rendering "http://localhost/" (Status code is 404).', $e->getMessage());

$previousException = $e->getPrevious();
$this->assertInstanceOf(HttpException::class, $previousException);
$this->assertEquals(404, $previousException->getStatusCode());
$this->assertEquals(0, $previousException->getCode());
}
}

public function testRender()
Expand Down