2010-02-17 13:55:05 +00:00
|
|
|
<?php
|
|
|
|
|
2010-07-09 09:05:26 +01:00
|
|
|
namespace Symfony\Bundle\FrameworkBundle\Controller;
|
2010-02-17 13:55:05 +00:00
|
|
|
|
2010-07-09 09:05:26 +01:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Debug\ExceptionFormatter;
|
2010-07-09 08:26:22 +01:00
|
|
|
use Symfony\Components\HttpFoundation\Request;
|
|
|
|
use Symfony\Components\HttpFoundation\Response;
|
2010-05-06 11:04:50 +01:00
|
|
|
use Symfony\Components\HttpKernel\Exception\HttpException;
|
2010-02-17 13:55:05 +00:00
|
|
|
|
|
|
|
/*
|
2010-04-07 01:51:29 +01:00
|
|
|
* This file is part of the Symfony framework.
|
2010-02-17 13:55:05 +00:00
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
|
|
|
* This source file is subject to the MIT license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-07 02:07:59 +01:00
|
|
|
* ExceptionController.
|
2010-02-17 13:55:05 +00:00
|
|
|
*
|
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*/
|
|
|
|
class ExceptionController extends Controller
|
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
2010-08-14 21:03:02 +01:00
|
|
|
* Converts an Exception to a Response.
|
|
|
|
*
|
|
|
|
* @param \Exception $exception An Exception instance
|
|
|
|
* @param Request $request The original Request instance
|
|
|
|
* @param array $logs An array of logs
|
|
|
|
*
|
2010-05-06 12:25:53 +01:00
|
|
|
* @throws \InvalidArgumentException When the exception template does not exist
|
|
|
|
*/
|
|
|
|
public function exceptionAction(\Exception $exception, Request $originalRequest, array $logs)
|
|
|
|
{
|
|
|
|
$template = $this->container->getParameter('kernel.debug') ? 'exception' : 'error';
|
2010-02-17 13:55:05 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$format = $format = $originalRequest->getRequestFormat();
|
2010-02-17 13:55:05 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
// when using CLI, we force the format to be TXT
|
2010-05-07 15:09:11 +01:00
|
|
|
if (0 === strncasecmp(PHP_SAPI, 'cli', 3)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$format = 'txt';
|
|
|
|
}
|
2010-02-17 13:55:05 +00:00
|
|
|
|
2010-08-14 21:03:02 +01:00
|
|
|
$template = $this['templating']->getLoader()->load($template, array(
|
2010-07-09 09:05:26 +01:00
|
|
|
'bundle' => 'FrameworkBundle',
|
2010-05-06 12:25:53 +01:00
|
|
|
'controller' => 'Exception',
|
|
|
|
'format' => '.'.$format,
|
|
|
|
));
|
2010-02-17 13:55:05 +00:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (false === $template) {
|
2010-05-06 12:25:53 +01:00
|
|
|
throw new \InvalidArgumentException(sprintf('The exception template for format "%s" does not exist.', $format));
|
|
|
|
}
|
2010-02-17 13:55:05 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$code = $exception instanceof HttpException ? $exception->getCode() : 500;
|
|
|
|
$text = Response::$statusTexts[$code];
|
2010-08-14 21:39:58 +01:00
|
|
|
$formatter = new ExceptionFormatter($this->container->getParameterBag()->has('debug.file_link_format') ? $this->container->getParameter('debug.file_link_format') : null, $this->container->getParameter('kernel.charset'));
|
2010-05-06 12:25:53 +01:00
|
|
|
$message = null === $exception->getMessage() ? 'n/a' : $exception->getMessage();
|
|
|
|
$name = get_class($exception);
|
|
|
|
$traces = $formatter->getTraces($exception, 'html' === $format ? 'html' : 'text');
|
|
|
|
$charset = $this->container->getParameter('kernel.charset');
|
2010-02-17 13:55:05 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$errors = 0;
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($logs as $log) {
|
2010-05-08 14:32:30 +01:00
|
|
|
if ('ERR' === $log['priorityName']) {
|
2010-05-06 12:25:53 +01:00
|
|
|
++$errors;
|
|
|
|
}
|
|
|
|
}
|
2010-02-17 13:55:05 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$currentContent = '';
|
2010-05-08 14:53:22 +01:00
|
|
|
while (false !== $content = ob_get_clean()) {
|
|
|
|
$currentContent .= $content;
|
|
|
|
}
|
2010-02-17 13:55:05 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
ob_start();
|
|
|
|
require $template;
|
|
|
|
$content = ob_get_clean();
|
2010-02-17 13:55:05 +00:00
|
|
|
|
2010-08-14 21:03:02 +01:00
|
|
|
$response = $this['response'];
|
2010-05-06 12:25:53 +01:00
|
|
|
$response->setStatusCode($code);
|
|
|
|
$response->setContent($content);
|
2010-02-17 13:55:05 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return $response;
|
|
|
|
}
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|