ExceptionController.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace Symfony\Bundle\FrameworkBundle\Controller;
  3. use Symfony\Component\DependencyInjection\ContainerAware;
  4. use Symfony\Component\HttpKernel\Exception\FlattenException;
  5. use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
  6. /*
  7. * This file is part of the Symfony framework.
  8. *
  9. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  10. *
  11. * This source file is subject to the MIT license that is bundled
  12. * with this source code in the file LICENSE.
  13. */
  14. /**
  15. * ExceptionController.
  16. *
  17. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  18. */
  19. class ExceptionController extends ContainerAware
  20. {
  21. /**
  22. * Converts an Exception to a Response.
  23. *
  24. * @param FlattenException $exception A FlattenException instance
  25. * @param DebugLoggerInterface $logger A DebugLoggerInterface instance
  26. * @param string $format The format to use for rendering (html, xml, ...)
  27. *
  28. * @throws \InvalidArgumentException When the exception template does not exist
  29. */
  30. public function exceptionAction(FlattenException $exception, DebugLoggerInterface $logger = null, $format = 'html')
  31. {
  32. $this->container->get('request')->setRequestFormat($format);
  33. $currentContent = '';
  34. while (false !== $content = @ob_get_clean()) {
  35. $currentContent .= $content;
  36. }
  37. if ('Symfony\Component\Security\Exception\AccessDeniedException' === $exception->getClass()) {
  38. $exception->setStatusCode($exception->getCode());
  39. }
  40. $template = $this->container->get('kernel')->isDebug() ? 'exception' : 'error';
  41. if ($this->container->get('kernel')->isDebug() && 'html' == $format) {
  42. $template = 'exception_full';
  43. }
  44. $template = 'FrameworkBundle:Exception:'.$template.'.twig';
  45. $templating = $this->container->get('templating');
  46. if (!$templating->exists($template)) {
  47. $this->container->get('request')->setRequestFormat('html');
  48. }
  49. $response = $templating->renderResponse(
  50. $template,
  51. array(
  52. 'exception' => $exception,
  53. 'logger' => $logger,
  54. 'currentContent' => $currentContent,
  55. )
  56. );
  57. $response->setStatusCode($exception->getStatusCode());
  58. return $response;
  59. }
  60. }