* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\HttpKernel\Debug; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\FlattenException; use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; /** * ExceptionHandler converts an exception to a Response object. * * It is mostly useful in debug mode to replace the default PHP/XDebug * output with something prettier and more useful. * * As this class is mainly used during Kernel boot, where nothing is yet * available, the Response content is always HTML. * * @author Fabien Potencier */ class ExceptionHandler { private $debug; public function __construct($debug = true) { $this->debug = $debug; } /** * Register the exception handler. * * @return The registered exception handler */ static public function register($debug = true) { $handler = new static($debug); set_exception_handler(array($handler, 'handle')); return $handler; } /** * Sends a Response for the given Exception. * * @param \Exception $exception An \Exception instance */ public function handle(\Exception $exception) { $this->createResponse($exception)->send(); } /** * Creates the error Response associated with the given Exception. * * @param \Exception $exception An \Exception instance * * @return Response A Response instance */ public function createResponse(\Exception $exception) { $content = ''; $title = ''; try { $code = $exception instanceof HttpExceptionInterface ? $exception->getStatusCode() : 500; $exception = FlattenException::create($exception); switch($code) { case 404: $title = 'Sorry, the page you are looking for could not be found.'; break; default: $title = 'Whoops, looks like something went wrong.'; } if ($this->debug) { $content = $this->getContent($exception); } } catch (\Exception $e) { // something nasty happened and we cannot throw an exception here anymore if ($this->debug) { $title = sprintf('Exception thrown when handling an exception (%s: %s)', get_class($exception), $exception->getMessage()); } else { $title = 'Whoops, looks like something went wrong.'; } } return new Response($this->decorate($content, $title), $code); } private function getContent($exception) { $message = nl2br($exception->getMessage()); $class = $this->abbrClass($exception->getClass()); $count = count($exception->getAllPrevious()); $content = ''; foreach ($exception->toArray() as $position => $e) { $ind = $count - $position + 1; $total = $count + 1; $class = $this->abbrClass($e['class']); $message = nl2br($e['message']); $content .= "

$ind/$total $class: $message

    "; foreach ($e['trace'] as $i => $trace) { $content .= '
  1. '; if ($trace['function']) { $content .= sprintf('at %s%s%s()', $this->abbrClass($trace['class']), $trace['type'], $trace['function']); } if (isset($trace['file']) && isset($trace['line'])) { $content .= sprintf(' in %s line %s', $trace['file'], $trace['line']); } $content .= '
  2. '; } $content .= '
'; } return $content; } private function decorate($content, $title) { return << {$title}

$title

$content
EOF; } private function abbrClass($class) { $parts = explode('\\', $class); return sprintf("%s", $class, array_pop($parts)); } }