ExceptionController.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. use Symfony\Component\OutputEscaper\SafeDecorator;
  7. /*
  8. * This file is part of the Symfony framework.
  9. *
  10. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  11. *
  12. * This source file is subject to the MIT license that is bundled
  13. * with this source code in the file LICENSE.
  14. */
  15. /**
  16. * ExceptionController.
  17. *
  18. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  19. */
  20. class ExceptionController extends ContainerAware
  21. {
  22. /**
  23. * Converts an Exception to a Response.
  24. *
  25. * @param FlattenException $exception A FlattenException instance
  26. * @param DebugLoggerInterface $logger A DebugLoggerInterface instance
  27. * @param string $format The format to use for rendering (html, xml, ...)
  28. * @param Boolean $embedded Whether the rendered Response will be embedded or not
  29. *
  30. * @throws \InvalidArgumentException When the exception template does not exist
  31. */
  32. public function exceptionAction(FlattenException $exception, DebugLoggerInterface $logger = null, $format = 'html', $embedded = false)
  33. {
  34. $this->container->get('request')->setRequestFormat($format);
  35. $currentContent = '';
  36. while (false !== $content = ob_get_clean()) {
  37. $currentContent .= $content;
  38. }
  39. $response = $this->container->get('templating')->renderResponse(
  40. 'FrameworkBundle:Exception:'.($this->container->get('kernel')->isDebug() ? 'exception.php' : 'error.php'),
  41. array(
  42. 'exception' => new SafeDecorator($exception),
  43. 'logger' => $logger,
  44. 'currentContent' => $currentContent,
  45. 'embedded' => $embedded,
  46. )
  47. );
  48. $response->setStatusCode($exception->getStatusCode());
  49. return $response;
  50. }
  51. }