Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Add an action to show *error* pages in kernel.debug mode #12096

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 6 commits 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
54 changes: 43 additions & 11 deletions 54 src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* ExceptionController.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Matthias Pigulla <mp@webfactory.de>
*/
class ExceptionController
{
Expand Down Expand Up @@ -48,18 +49,24 @@ public function showAction(Request $request, FlattenException $exception, DebugL
{
$currentContent = $this->getAndCleanOutputBuffering($request->headers->get('X-Php-Ob-Level', -1));

$code = $exception->getStatusCode();
return $this->createResponse($request, $exception, $this->debug, $logger, $currentContent);
}

return new Response($this->twig->render(
$this->findTemplate($request, $request->getRequestFormat(), $code, $this->debug),
array(
'status_code' => $code,
'status_text' => isset(Response::$statusTexts[$code]) ? Response::$statusTexts[$code] : '',
'exception' => $exception,
'logger' => $logger,
'currentContent' => $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);

return $this->createResponse($request, $exception, false);
}

/**
Expand Down Expand Up @@ -130,4 +137,29 @@ 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 = '')
Copy link
Member

Choose a reason for hiding this comment

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

I think this should be private

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was in the meantime, but I reverted it back to protected.

Reason: The docs mention subclassing this controller in order to pass additional variables to the template. As this part of the code as moved from the showAction to this method, I'd like to keep the possibility to override it.

@stof agree?

{
$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,
)
));
}
}
12 changes: 12 additions & 0 deletions 12 src/Symfony/Bundle/TwigBundle/Resources/config/routing/errors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>

<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">

<route id="_twig_error_test" path="/{code}.{_format}">
<default key="_controller">twig.controller.exception:testErrorPageAction</default>
<default key="_format">html</default>
<requirement key="code">\d+</requirement>
</route>
</routes>
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,44 @@ public function testOnlyClearOwnOutputBuffers()
$controller = new ExceptionController($twig, false);
$controller->showAction($request, $flatten);
}

public function testErrorPagesInDebugMode()
{
$twig = new \Twig_Environment(
new \Twig_Loader_Array(array(
'TwigBundle:Exception:error404.html.twig' => '
{%- if exception is defined and status_text is defined and status_code is defined -%}
OK
{%- else -%}
"exception" variable is missing
{%- endif -%}
',
))
);

$request = Request::create('whatever');

$controller = new ExceptionController($twig, /* "debug" set to --> */ true);
$response = $controller->testErrorPageAction($request, 404);

$this->assertEquals(200, $response->getStatusCode()); // successful request
$this->assertEquals('OK', $response->getContent()); // content of the error404.html template
}

public function testFallbackToHtmlIfNoTemplateForRequestedFormat()
{
$twig = new \Twig_Environment(
new \Twig_Loader_Array(array(
'TwigBundle:Exception:error.html.twig' => 'html',
))
);

$request = Request::create('whatever');
$request->setRequestFormat('txt');

$controller = new ExceptionController($twig, false);
$response = $controller->testErrorPageAction($request, 42);

$this->assertEquals('html', $request->getRequestFormat());
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.