-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[TwigBundle] Fix error page preview for custom twig.exception_controller #12147
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
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,14 +18,19 @@ | |
use Symfony\Component\HttpFoundation\Response; | ||
|
||
/** | ||
* ExceptionController. | ||
* ExceptionController renders error or exception pages for a given | ||
* FlattenException. | ||
* | ||
* @author Fabien Potencier <[email protected]> | ||
* @author Matthias Pigulla <[email protected]> | ||
*/ | ||
class ExceptionController | ||
{ | ||
protected $twig; | ||
|
||
/** | ||
* @var bool Show error (false) or exception (true) pages by default. | ||
*/ | ||
protected $debug; | ||
|
||
public function __construct(\Twig_Environment $twig, $debug) | ||
|
@@ -37,6 +42,10 @@ public function __construct(\Twig_Environment $twig, $debug) | |
/** | ||
* Converts an Exception to a Response. | ||
* | ||
* A "showException" request parameter can be used to force display of an error page (when set to false) or | ||
* the exception page (when true). If it is not present, the "debug" value passed into the constructor will | ||
* be used. | ||
* | ||
* @param Request $request The request | ||
* @param FlattenException $exception A FlattenException instance | ||
* @param DebugLoggerInterface $logger A DebugLoggerInterface instance | ||
|
@@ -48,25 +57,20 @@ public function __construct(\Twig_Environment $twig, $debug) | |
public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null) | ||
{ | ||
$currentContent = $this->getAndCleanOutputBuffering($request->headers->get('X-Php-Ob-Level', -1)); | ||
$showException = $request->get('showException', $this->debug); // As opposed to an additional parameter, this maintains BC | ||
|
||
return $this->createResponse($request, $exception, $this->debug, $logger, $currentContent); | ||
} | ||
|
||
/** | ||
* Displays the error page for arbitrary status codes and formats. | ||
* | ||
* @param Request $request The request | ||
* @param int $code The HTTP status code to show the error page for. | ||
* | ||
* @return Response | ||
* | ||
* @throws \InvalidArgumentException When the error template does not exist | ||
*/ | ||
public function testErrorPageAction(Request $request, $code) | ||
{ | ||
$exception = FlattenException::create(new \Exception("Something has intentionally gone wrong."), $code); | ||
$code = $exception->getStatusCode(); | ||
|
||
return $this->createResponse($request, $exception, false); | ||
return new Response($this->twig->render( | ||
$this->findTemplate($request, $request->getRequestFormat(), $code, $showException), | ||
array( | ||
'status_code' => $code, | ||
'status_text' => isset(Response::$statusTexts[$code]) ? Response::$statusTexts[$code] : '', | ||
'exception' => $exception, | ||
'logger' => $logger, | ||
'currentContent' => $currentContent, | ||
) | ||
)); | ||
} | ||
|
||
/** | ||
|
@@ -89,19 +93,19 @@ protected function getAndCleanOutputBuffering($startObLevel) | |
* @param Request $request | ||
* @param string $format | ||
* @param int $code An HTTP response status code | ||
* @param bool $debug | ||
* @param bool $showException | ||
* | ||
* @return TemplateReference | ||
*/ | ||
protected function findTemplate(Request $request, $format, $code, $debug) | ||
protected function findTemplate(Request $request, $format, $code, $showException) | ||
{ | ||
$name = $debug ? 'exception' : 'error'; | ||
if ($debug && 'html' == $format) { | ||
$name = $showException ? 'exception' : 'error'; | ||
if ($showException && 'html' == $format) { | ||
$name = 'exception_full'; | ||
} | ||
|
||
// when not in debug, try to find a template for the specific HTTP status code and format | ||
if (!$debug) { | ||
// For error pages, try to find a template for the specific HTTP status code and format | ||
if (!$showException) { | ||
$template = new TemplateReference('TwigBundle', 'Exception', $name.$code, $format, 'twig'); | ||
if ($this->templateExists($template)) { | ||
return $template; | ||
|
@@ -137,29 +141,4 @@ protected function templateExists($template) | |
|
||
return false; | ||
} | ||
|
||
/** | ||
* @param Request $request | ||
* @param FlattenException $exception | ||
* @param bool $debug | ||
* @param DebugLoggerInterface $logger | ||
* @param string $currentContent | ||
* | ||
* @return Response | ||
*/ | ||
protected function createResponse(Request $request, FlattenException $exception, $debug, DebugLoggerInterface $logger = null, $currentContent = '') | ||
{ | ||
$code = $exception->getStatusCode(); | ||
|
||
return new Response($this->twig->render( | ||
$this->findTemplate($request, $request->getRequestFormat(), $code, $debug), | ||
array( | ||
'status_code' => $code, | ||
'status_text' => isset(Response::$statusTexts[$code]) ? Response::$statusTexts[$code] : '', | ||
'exception' => $exception, | ||
'logger' => $logger, | ||
'currentContent' => $currentContent, | ||
) | ||
)); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/Symfony/Bundle/TwigBundle/Controller/PreviewErrorController.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\TwigBundle\Controller; | ||
|
||
use Symfony\Component\HttpKernel\Exception\FlattenException; | ||
use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
* PreviewErrorController can be used to test error pages. | ||
* | ||
* It will create a test exception and forward it to another controller. | ||
* | ||
* @author Matthias Pigulla <[email protected]> | ||
*/ | ||
class PreviewErrorController | ||
{ | ||
protected $kernel; | ||
protected $controller; | ||
|
||
public function __construct(HttpKernelInterface $kernel, $controller) | ||
{ | ||
$this->kernel = $kernel; | ||
$this->controller = $controller; | ||
} | ||
|
||
public function previewErrorPageAction(Request $request, $code) | ||
{ | ||
$exception = FlattenException::create(new \Exception("Something has intentionally gone wrong."), $code); | ||
|
||
/* | ||
* This Request mimics the parameters set by | ||
* \Symfony\Component\HttpKernel\EventListener\ExceptionListener::duplicateRequest, with | ||
* the additional "showException" flag. | ||
*/ | ||
|
||
$subRequest = $request->duplicate(null, null, array( | ||
'_controller' => $this->controller, | ||
'exception' => $exception, | ||
'logger' => null, | ||
'format' => $request->getRequestFormat(), | ||
'showException' => false, | ||
)); | ||
|
||
return $this->kernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/Symfony/Bundle/TwigBundle/Tests/Controller/PreviewErrorControllerTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\TwigBundle\Tests\Controller; | ||
|
||
use Symfony\Bundle\TwigBundle\Controller\PreviewErrorController; | ||
use Symfony\Bundle\TwigBundle\Tests\TestCase; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
|
||
class PreviewErrorControllerTest extends TestCase | ||
{ | ||
public function testForwardRequestToConfiguredController() | ||
{ | ||
$self = $this; | ||
|
||
$request = Request::create('whatever'); | ||
$response = new Response(""); | ||
$code = 123; | ||
$logicalControllerName = 'foo:bar:baz'; | ||
|
||
$kernel = $this->getMock('\Symfony\Component\HttpKernel\HttpKernelInterface'); | ||
$kernel | ||
->expects($this->once()) | ||
->method('handle') | ||
->with( | ||
$this->callback(function (Request $request) use ($self, $logicalControllerName, $code) { | ||
|
||
$self->assertEquals($logicalControllerName, $request->attributes->get('_controller')); | ||
|
||
$exception = $request->attributes->get('exception'); | ||
$self->assertInstanceOf('Symfony\Component\HttpKernel\Exception\FlattenException', $exception); | ||
$self->assertEquals($code, $exception->getStatusCode()); | ||
|
||
$self->assertFalse($request->attributes->get('showException')); | ||
|
||
return true; | ||
}), | ||
$this->equalTo(HttpKernelInterface::SUB_REQUEST) | ||
) | ||
->will($this->returnValue($response)); | ||
|
||
$controller = new PreviewErrorController($kernel, $logicalControllerName); | ||
|
||
$this->assertSame($response, $controller->previewErrorPageAction($request, $code)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be
$request->attributes->get('showException'
actually