ExceptionController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace Symfony\Bundle\FrameworkBundle\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller;
  4. use Symfony\Bundle\FrameworkBundle\Debug\ExceptionFormatter;
  5. use Symfony\Components\HttpFoundation\Request;
  6. use Symfony\Components\HttpFoundation\Response;
  7. use Symfony\Components\HttpKernel\Exception\HttpException;
  8. /*
  9. * This file is part of the Symfony framework.
  10. *
  11. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  12. *
  13. * This source file is subject to the MIT license that is bundled
  14. * with this source code in the file LICENSE.
  15. */
  16. /**
  17. * ExceptionController.
  18. *
  19. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  20. */
  21. class ExceptionController extends Controller
  22. {
  23. /**
  24. * Converts an Exception to a Response.
  25. *
  26. * @param \Exception $exception An Exception instance
  27. * @param Request $request The original Request instance
  28. * @param array $logs An array of logs
  29. *
  30. * @throws \InvalidArgumentException When the exception template does not exist
  31. */
  32. public function exceptionAction(\Exception $exception, Request $originalRequest, array $logs)
  33. {
  34. $template = $this->container->getParameter('kernel.debug') ? 'exception' : 'error';
  35. $format = $format = $originalRequest->getRequestFormat();
  36. // when using CLI, we force the format to be TXT
  37. if (0 === strncasecmp(PHP_SAPI, 'cli', 3)) {
  38. $format = 'txt';
  39. }
  40. $template = $this['templating']->getLoader()->load($template, array(
  41. 'bundle' => 'FrameworkBundle',
  42. 'controller' => 'Exception',
  43. 'format' => '.'.$format,
  44. ));
  45. if (false === $template) {
  46. throw new \InvalidArgumentException(sprintf('The exception template for format "%s" does not exist.', $format));
  47. }
  48. $code = $exception instanceof HttpException ? $exception->getCode() : 500;
  49. $text = Response::$statusTexts[$code];
  50. $formatter = new ExceptionFormatter($this->container->getParameterBag()->has('debug.file_link_format') ? $this->container->getParameter('debug.file_link_format') : null, $this->container->getParameter('kernel.charset'));
  51. $message = null === $exception->getMessage() ? 'n/a' : $exception->getMessage();
  52. $name = get_class($exception);
  53. $traces = $formatter->getTraces($exception, 'html' === $format ? 'html' : 'text');
  54. $charset = $this->container->getParameter('kernel.charset');
  55. $errors = 0;
  56. foreach ($logs as $log) {
  57. if ('ERR' === $log['priorityName']) {
  58. ++$errors;
  59. }
  60. }
  61. $currentContent = '';
  62. while (false !== $content = ob_get_clean()) {
  63. $currentContent .= $content;
  64. }
  65. ob_start();
  66. require $template;
  67. $content = ob_get_clean();
  68. $response = $this['response'];
  69. $response->setStatusCode($code);
  70. $response->setContent($content);
  71. return $response;
  72. }
  73. }