diff --git a/src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php b/src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php index 33a5507c7ea1e..18dfb6a50f5ff 100644 --- a/src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php +++ b/src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php @@ -21,6 +21,7 @@ * ExceptionController. * * @author Fabien Potencier + * @author Matthias Pigulla */ class ExceptionController { @@ -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); } /** @@ -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 = '') + { + $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, + ) + )); + } } diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/routing/errors.xml b/src/Symfony/Bundle/TwigBundle/Resources/config/routing/errors.xml new file mode 100644 index 0000000000000..493af74c019d9 --- /dev/null +++ b/src/Symfony/Bundle/TwigBundle/Resources/config/routing/errors.xml @@ -0,0 +1,12 @@ + + + + + + twig.controller.exception:testErrorPageAction + html + \d+ + + diff --git a/src/Symfony/Bundle/TwigBundle/Tests/Controller/ExceptionControllerTest.php b/src/Symfony/Bundle/TwigBundle/Tests/Controller/ExceptionControllerTest.php index 18523eaa76732..39e17cb221ee9 100644 --- a/src/Symfony/Bundle/TwigBundle/Tests/Controller/ExceptionControllerTest.php +++ b/src/Symfony/Bundle/TwigBundle/Tests/Controller/ExceptionControllerTest.php @@ -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()); + } }