From 02088004592d4704141f38257657755bf3a5b9f6 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 29 Aug 2010 11:52:55 +0200 Subject: [PATCH] refactored exception management (removed the ExceptionManager) --- .../Controller/ExceptionController.php | 19 +- .../Debug/ExceptionListener.php | 7 +- .../Debug/ExceptionManager.php | 136 -------------- .../FrameworkBundle/Resources/config/web.xml | 1 - .../Resources/views/Exception/error.atom.php | 2 +- .../Resources/views/Exception/error.css.php | 2 +- .../Resources/views/Exception/error.js.php | 2 +- .../Resources/views/Exception/error.json.php | 4 +- .../Resources/views/Exception/error.php | 2 +- .../Resources/views/Exception/error.rdf.php | 2 +- .../Resources/views/Exception/error.txt.php | 2 +- .../Resources/views/Exception/error.xml.php | 2 +- .../views/Exception/exception.atom.php | 2 +- .../views/Exception/exception.css.php | 2 +- .../views/Exception/exception.js.php | 2 +- .../views/Exception/exception.json.php | 18 +- .../Resources/views/Exception/exception.php | 43 ++--- .../views/Exception/exception.txt.php | 16 +- .../views/Exception/exception.xml.php | 14 +- .../Resources/views/Exception/layout.php | 2 +- .../Resources/views/Exception/traces.php | 4 +- .../Resources/views/Exception/traces.txt.php | 4 +- .../Resources/views/Exception/traces.xml.php | 2 +- .../Templating/Helper/CodeHelper.php | 55 +++--- .../Bundle/ZendBundle/Logger/DebugLogger.php | 15 ++ .../DataCollector/ExceptionDataCollector.php | 3 +- .../HttpKernel/Exception/FlattenException.php | 167 ++++++++++++++++++ .../HttpKernel/Log/DebugLoggerInterface.php | 7 + 28 files changed, 298 insertions(+), 239 deletions(-) delete mode 100644 src/Symfony/Bundle/FrameworkBundle/Debug/ExceptionManager.php create mode 100644 src/Symfony/Component/HttpKernel/Exception/FlattenException.php diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/ExceptionController.php b/src/Symfony/Bundle/FrameworkBundle/Controller/ExceptionController.php index cf9ccca1d8..a8d03d58c2 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Controller/ExceptionController.php +++ b/src/Symfony/Bundle/FrameworkBundle/Controller/ExceptionController.php @@ -3,7 +3,9 @@ namespace Symfony\Bundle\FrameworkBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; -use Symfony\Bundle\FrameworkBundle\Debug\ExceptionManager; +use Symfony\Component\HttpKernel\Exception\FlattenException; +use Symfony\Component\HttpKernel\Log\DebugLoggerInterface; +use Symfony\Component\OutputEscaper\SafeDecorator; /* * This file is part of the Symfony framework. @@ -24,13 +26,14 @@ class ExceptionController extends Controller /** * Converts an Exception to a Response. * - * @param ExceptionManager $manager An ExceptionManager instance - * @param string $format The format to use for rendering (html, xml, ...) - * @param Boolean $embedded Whether the rendered Response will be embedded or not + * @param FlattenException $exception A FlattenException instance + * @param DebugLoggerInterface $logger A DebugLoggerInterface instance + * @param string $format The format to use for rendering (html, xml, ...) + * @param Boolean $embedded Whether the rendered Response will be embedded or not * * @throws \InvalidArgumentException When the exception template does not exist */ - public function exceptionAction(ExceptionManager $manager, $format, $embedded = false) + public function exceptionAction(FlattenException $exception, DebugLoggerInterface $logger, $format, $embedded = false) { $this['request']->setRequestFormat($format); @@ -42,13 +45,13 @@ class ExceptionController extends Controller $response = $this->render( 'FrameworkBundle:Exception:'.($this['kernel']->isDebug() ? 'exception' : 'error'), array( - 'manager' => $manager, - 'managers' => $manager->getLinkedManagers(), + 'exception' => new SafeDecorator($exception), + 'logger' => $logger, 'currentContent' => $currentContent, 'embedded' => $embedded, ) ); - $response->setStatusCode($manager->getStatusCode()); + $response->setStatusCode($exception->getStatusCode()); return $response; } diff --git a/src/Symfony/Bundle/FrameworkBundle/Debug/ExceptionListener.php b/src/Symfony/Bundle/FrameworkBundle/Debug/ExceptionListener.php index a1e76c0c61..8baf6c8128 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Debug/ExceptionListener.php +++ b/src/Symfony/Bundle/FrameworkBundle/Debug/ExceptionListener.php @@ -7,6 +7,7 @@ use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\Event; use Symfony\Component\HttpKernel\Log\LoggerInterface; use Symfony\Component\HttpKernel\HttpKernelInterface; +use Symfony\Component\HttpKernel\Exception\FlattenException; use Symfony\Component\HttpFoundation\Request; /* @@ -62,12 +63,12 @@ class ExceptionListener error_log(sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine())); } - $class = $this->container->getParameter('exception_manager.class'); - $logger = $this->container->has('logger.debug') ? $this->container->get('logger.debug') : null; + $logger = $this->container->has('logger') ? $this->container->get('logger')->getDebugLogger() : null; $attributes = array( '_controller' => $this->controller, - 'manager' => new $class($exception, $logger), + 'exception' => FlattenException::create($exception), + 'logger' => $logger, // when using CLI, we force the format to be TXT 'format' => 0 === strncasecmp(PHP_SAPI, 'cli', 3) ? 'txt' : $request->getRequestFormat(), ); diff --git a/src/Symfony/Bundle/FrameworkBundle/Debug/ExceptionManager.php b/src/Symfony/Bundle/FrameworkBundle/Debug/ExceptionManager.php deleted file mode 100644 index 8a73d4dcc5..0000000000 --- a/src/Symfony/Bundle/FrameworkBundle/Debug/ExceptionManager.php +++ /dev/null @@ -1,136 +0,0 @@ - - * - * This source file is subject to the MIT license that is bundled - * with this source code in the file LICENSE. - */ - -/** - * ExceptionManager. - * - * @author Fabien Potencier - */ -class ExceptionManager -{ - protected $exception; - protected $logger; - - public function __construct(\Exception $exception, DebugLoggerInterface $logger = null) - { - $this->exception = $exception; - $this->logger = $logger; - } - - public function getLinkedManagers() - { - $managers = array(); - $e = $this->exception; - while ($e = $e->getPrevious()) { - $managers[] = new $this($e); - } - - return $managers; - } - - public function getException() - { - return $this->exception; - } - - public function getLogger() - { - return $this->logger; - } - - public function getLogs() - { - return null === $this->logger ? array() : $this->logger->getLogs(); - } - - public function countErrors() - { - if (null === $this->logger) { - return 0; - } - - $errors = 0; - foreach ($this->logger->getLogs() as $log) { - if ('ERR' === $log['priorityName']) { - ++$errors; - } - } - - return $errors; - } - - public function getStatusCode() - { - return $this->exception instanceof HttpException ? $this->exception->getCode() : 500; - } - - public function getStatusText() - { - return Response::$statusTexts[$this->getStatusCode()]; - } - - public function getMessage() - { - return null === $this->exception->getMessage() ? 'n/a' : $this->exception->getMessage(); - } - - public function getName() - { - return get_class($this->exception); - } - - /** - * Returns an array of exception traces. - * - * @return array An array of traces - */ - public function getTraces() - { - $traces = array(); - $traces[] = array( - 'class' => '', - 'type' => '', - 'function' => '', - 'file' => $this->exception->getFile(), - 'line' => $this->exception->getLine(), - 'args' => array(), - ); - foreach ($this->exception->getTrace() as $entry) { - $class = ''; - $namespace = ''; - if (isset($entry['class'])) { - $parts = explode('\\', $entry['class']); - $class = array_pop($parts); - $namespace = implode('\\', $parts); - } - - $traces[] = array( - 'namespace' => $namespace, - 'short_class' => $class, - 'class' => isset($entry['class']) ? $entry['class'] : '', - 'type' => isset($entry['type']) ? $entry['type'] : '', - 'function' => $entry['function'], - 'file' => isset($entry['file']) ? $entry['file'] : null, - 'line' => isset($entry['line']) ? $entry['line'] : null, - 'args' => isset($entry['args']) ? $entry['args'] : array(), - ); - } - - return $traces; - } -} diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/web.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/web.xml index 7e202eaa5f..24b5ed0da6 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/web.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/web.xml @@ -11,7 +11,6 @@ Symfony\Component\HttpKernel\ResponseListener Symfony\Bundle\FrameworkBundle\Debug\ExceptionListener Symfony\Bundle\FrameworkBundle\Controller\ExceptionController::exceptionAction - Symfony\Bundle\FrameworkBundle\Debug\ExceptionManager Symfony\Component\HttpKernel\Cache\Esi Symfony\Component\HttpKernel\Cache\EsiListener diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.atom.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.atom.php index 9f586ebb0c..775e711799 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.atom.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.atom.php @@ -1 +1 @@ -render('FrameworkBundle:Exception:error.xml', array('manager' => $manager)) ?> +render('FrameworkBundle:Exception:error.xml', array('exception' => $exception)) ?> diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.css.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.css.php index de436e7949..d1bafffb97 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.css.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.css.php @@ -1 +1 @@ -render('FrameworkBundle:Exception:error.js', array('manager' => $manager)) ?> +render('FrameworkBundle:Exception:error.js', array('exception' => $exception)) ?> diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.js.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.js.php index 12a34ec549..49de1c18d5 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.js.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.js.php @@ -1,3 +1,3 @@ /* -getStatusCode().' '.$manager->getStatusText()."\n" ?> +getStatusCode().' '.$exception->getStatusText()."\n" ?> */ diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.json.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.json.php index 92a565c5d3..a70ca900f4 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.json.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.json.php @@ -1,5 +1,5 @@ array( - 'code' => $manager->getStatusCode(), - 'message' => $manager->getStatusText(), + 'code' => $exception->getStatusCode(), + 'message' => $exception->getStatusText(), ))) ?> diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.php index dda1ef4b9c..b979bf9740 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.php @@ -6,7 +6,7 @@

Oops! An Error Occurred

-

The server returned a "getStatusCode() ?> getStatusText() ?>".

+

The server returned a "getStatusCode() ?> getStatusText() ?>".

Something is broken. Please e-mail us at [email] and let us know diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.rdf.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.rdf.php index 9f586ebb0c..775e711799 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.rdf.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.rdf.php @@ -1 +1 @@ -render('FrameworkBundle:Exception:error.xml', array('manager' => $manager)) ?> +render('FrameworkBundle:Exception:error.xml', array('exception' => $exception)) ?> diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.txt.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.txt.php index 215ed6331d..bed3de7b15 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.txt.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.txt.php @@ -1,7 +1,7 @@ Oops! An Error Occurred ======================= -The server returned a "getStatusCode() ?> getStatusText() ?>". +The server returned a "getStatusCode() ?> getStatusText() ?>". Please e-mail us at [email] and let us know what you were doing when this error occurred. We will fix it as soon as possible. Sorry for any diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.xml.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.xml.php index 6d7ca3320d..a34cb82510 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.xml.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.xml.php @@ -1,2 +1,2 @@ ', $view->getCharset())."\n" ?> - + diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.atom.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.atom.php index aecec94f31..64f3410c29 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.atom.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.atom.php @@ -1 +1 @@ -render('FrameworkBundle:Exception:exception.xml', array('manager' => $manager)) ?> +render('FrameworkBundle:Exception:exception.xml', array('exception' => $exception)) ?> diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.css.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.css.php index e7e60595e6..24d39df8c4 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.css.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.css.php @@ -1,3 +1,3 @@ /* -render('FrameworkBundle:Exception:exception.txt', array('manager' => $manager)) ?> +render('FrameworkBundle:Exception:exception.txt', array('exception' => $exception)) ?> */ diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.js.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.js.php index e7e60595e6..24d39df8c4 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.js.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.js.php @@ -1,3 +1,3 @@ /* -render('FrameworkBundle:Exception:exception.txt', array('manager' => $manager)) ?> +render('FrameworkBundle:Exception:exception.txt', array('exception' => $exception)) ?> */ diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.json.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.json.php index 0e4dc5f687..eea6a4bce7 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.json.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.json.php @@ -2,22 +2,22 @@ $vars = array( 'error' => array( - 'code' => $manager->getStatusCode(), - 'message' => $manager->getName(), + 'code' => $exception->getStatusCode(), + 'message' => $exception->getMessage(), 'exception' => array( - 'name' => $manager->getName(), - 'message' => $manager->getMessage(), - 'traces' => $manager->getTraces(), + 'name' => $exception->getClass(), + 'message' => $exception->getMessage(), + 'trace' => $exception->getTrace(), ), )); -if (count($managers)) { +if (count($exception->getPreviouses())) { $vars['exceptions'] = array(); - foreach ($managers as $i => $previous) { + foreach ($exception->getPreviouses() as $i => $previous) { $vars['exceptions'][] = array( - 'name' => $previous->getName(), + 'name' => $previous->getClass(), 'message' => $previous->getMessage(), - 'traces' => $previous->getTraces(), + 'trace' => $previous->getTrace(), ); } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.php index 40b95c7d1f..0221728d47 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.php @@ -8,14 +8,15 @@
-

formatFileFromText(str_replace("\n", '
', htmlspecialchars($manager->getMessage(), ENT_QUOTES, $view->getCharset()))) ?>

-

getStatusCode() ?> getStatusText() ?> - getName() ?>

- -
linked Exception 1): ?>s: +

formatFileFromText(str_replace("\n", '
', htmlspecialchars($exception->getMessage(), ENT_QUOTES, $view->getCharset()))) ?>

+

getStatusCode() ?> getStatusText() ?> - getClass() ?>

+ + getPreviouses())): ?> +
linked Exception 1): ?>s:
    - $previous): ?> + getPreviouses() as $i => $previous): ?>
  • - getName() ?> » + getClass() ?> »
@@ -26,25 +27,27 @@
- render('FrameworkBundle:Exception:traces', array('manager' => $manager, 'position' => 0, 'count' => count($managers))) ?> + render('FrameworkBundle:Exception:traces', array('exception' => $exception, 'position' => 0, 'count' => $previousCount)) ?> - $previous): ?> - render('FrameworkBundle:Exception:traces', array('manager' => $previous, 'position' => $i + 1, 'count' => count($managers))) ?> + getPreviouses() as $i => $previous): ?> + render('FrameworkBundle:Exception:traces', array('exception' => $previous, 'position' => $i + 1, 'count' => $previousCount)) ?> -
-

- countErrors()): ?> - countErrors() ?> errorcountErrors() > 1): ?>s - - Logs » -

+ +
+

+ countErrors()): ?> + countErrors() ?> errorcountErrors() > 1): ?>s + + Logs » +

+ + - - -
+

Content of the Output »

diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.txt.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.txt.php index 9c4e22a689..7e345e5400 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.txt.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.txt.php @@ -1,16 +1,16 @@ -[exception] getStatusCode().' | '.$manager->getStatusText().' | '.$manager->getName() ?> +[exception] getStatusCode().' | '.$exception->getStatusText().' | '.$exception->getClass() ?> -[message] getMessage() ?> +[message] getMessage() ?> -getTraces())): ?> -render('FrameworkBundle:Exception:traces', array('manager' => $manager, 'position' => 0, 'count' => count($managers))) ?> +getPreviouses())): ?> +render('FrameworkBundle:Exception:traces.txt', array('exception' => $exception, 'position' => 0, 'count' => $previousCount)) ?> - - $previous): ?> -[linked exception] getName() ?>: getMessage() ?> + +getPreviouses() as $i => $previous): ?> +[linked exception] getClass() ?>: getMessage() ?> -render('FrameworkBundle:Exception:traces', array('manager' => $previous, 'position' => $i + 1, 'count' => count($managers))) ?> +render('FrameworkBundle:Exception:traces.txt', array('exception' => $previous, 'position' => $i + 1, 'count' => $previousCount)) ?> diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.xml.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.xml.php index cec8cd3701..c6b97cf9e0 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.xml.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.xml.php @@ -1,12 +1,12 @@ ', $view->getCharset())."\n" ?> - - - render('FrameworkBundle:Exception:traces', array('manager' => $manager, 'position' => 0, 'count' => count($managers))) ?> + + + render('FrameworkBundle:Exception:traces', array('exception' => $exception, 'position' => 0, 'count' => ($previousCount = count($exception->getPreviouses())))) ?> - - $previous): ?> - - render('FrameworkBundle:Exception:traces', array('manager' => $previous, 'position' => $i + 1, 'count' => count($managers))) ?> + +getPreviouses() as $i => $previous): ?> + + render('FrameworkBundle:Exception:traces', array('exception' => $previous, 'position' => $i + 1, 'count' => $previousCount)) ?> diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/layout.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/layout.php index 81f9036a1d..5de199732f 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/layout.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/layout.php @@ -2,7 +2,7 @@ - <?php echo htmlspecialchars($manager->getMessage(), ENT_QUOTES, $view->getCharset()) ?> (<?php echo $manager->getStatusCode() ?> <?php echo $manager->getStatusText() ?>) + <?php echo htmlspecialchars($exception->getMessage(), ENT_QUOTES, $view->getCharset()) ?> (<?php echo $exception->getStatusCode() ?> <?php echo $exception->getStatusText() ?>)