Skip to content

Commit 74b60be

Browse files
committed
feature #37537 [HttpKernel] Provide status code in fragment handler exception (gonzalovilaseca)
This PR was merged into the 5.2-dev branch. Discussion ---------- [HttpKernel] Provide status code in fragment handler exception | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | | License | MIT | Doc PR | We have a use case where it would be useful to retrieve the status code in an exception listener from the exception thrown by the fragment handler, current solution is to extract it from the exception string which is ugly. With this change we can get the status code from the exception directly. Commits ------- 81ca1f00a3 [HttpKernel] Provide status code in fragment handler exception
2 parents 8216716 + ea0b110 commit 74b60be

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

Fragment/FragmentHandler.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\HttpFoundation\Response;
1616
use Symfony\Component\HttpFoundation\StreamedResponse;
1717
use Symfony\Component\HttpKernel\Controller\ControllerReference;
18+
use Symfony\Component\HttpKernel\Exception\HttpException;
1819

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

103105
if (!$response instanceof StreamedResponse) {

Tests/Fragment/FragmentHandlerTest.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\HttpFoundation\Response;
17+
use Symfony\Component\HttpKernel\Exception\HttpException;
1718
use Symfony\Component\HttpKernel\Fragment\FragmentHandler;
1819

1920
/**
@@ -53,11 +54,20 @@ public function testRenderWithUnknownRenderer()
5354

5455
public function testDeliverWithUnsuccessfulResponse()
5556
{
56-
$this->expectException('RuntimeException');
57-
$this->expectExceptionMessage('Error when rendering "http://localhost/" (Status code is 404).');
5857
$handler = $this->getHandler($this->returnValue(new Response('foo', 404)));
59-
60-
$handler->render('/', 'foo');
58+
try {
59+
$handler->render('/', 'foo');
60+
$this->fail('->render() throws a \RuntimeException exception if response is not successful');
61+
} catch (\Exception $e) {
62+
$this->assertInstanceOf('\RuntimeException', $e);
63+
$this->assertEquals(0, $e->getCode());
64+
$this->assertEquals('Error when rendering "http://localhost/" (Status code is 404).', $e->getMessage());
65+
66+
$previousException = $e->getPrevious();
67+
$this->assertInstanceOf(HttpException::class, $previousException);
68+
$this->assertEquals(404, $previousException->getStatusCode());
69+
$this->assertEquals(0, $previousException->getCode());
70+
}
6171
}
6272

6373
public function testRender()

0 commit comments

Comments
 (0)