Permalink
Browse files

[HttpKernel] made the ExceptionHandler more flexible

  • Loading branch information...
1 parent a086cdc commit fbf19f0fbffe44d84c6c2af15c62d649df0be6f9 @fabpot fabpot committed Oct 4, 2012
Showing with 103 additions and 82 deletions.
  1. +103 −82 src/Symfony/Component/HttpKernel/Debug/ExceptionHandler.php
View
185 src/Symfony/Component/HttpKernel/Debug/ExceptionHandler.php
@@ -73,100 +73,105 @@ public function handle(\Exception $exception)
*/
public function createResponse($exception)
{
- $content = '';
+ if (!$exception instanceof FlattenException) {
+ $exception = FlattenException::create($exception);
+ }
+
+ return new Response($this->decorate($this->getContent($exception), $this->getStylesheet($exception)), $exception->getStatusCode(), $exception->getHeaders());
+ }
+
+ /**
+ * Gets the HTML content associated with the given exception.
+ *
+ * @param FlattenException $exception A FlattenException instance
+ *
+ * @return string The content as a string
+ */
+ public function getContent(FlattenException $exception)
+ {
+ if (!$this->debug) {
+ return '';
+ }
+
$title = '';
+ switch ($exception->getStatusCode()) {
+ case 404:
+ $title = 'Sorry, the page you are looking for could not be found.';
+ break;
+ default:
+ $title = 'Whoops, looks like something went wrong.';
+ }
+
try {
- if (!$exception instanceof FlattenException) {
- $exception = FlattenException::create($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 .= sprintf(<<<EOF
+ <div class="block_exception clear_fix">
+ <h2><span>%d/%d</span> %s: %s</h2>
+ </div>
+ <div class="block">
+ <ol class="traces list_exception">
- switch ($exception->getStatusCode()) {
- case 404:
- $title = 'Sorry, the page you are looking for could not be found.';
- break;
- default:
- $title = 'Whoops, looks like something went wrong.';
- }
+EOF
+ , $ind, $total, $class, $message);
+ foreach ($e['trace'] as $i => $trace) {
+ $content .= ' <li>';
+ if ($trace['function']) {
+ $content .= sprintf('at %s%s%s(%s)', $this->abbrClass($trace['class']), $trace['type'], $trace['function'], $this->formatArgs($trace['args']));
+ }
+ if (isset($trace['file']) && isset($trace['line'])) {
+ if ($linkFormat = ini_get('xdebug.file_link_format')) {
+ $link = str_replace(array('%f', '%l'), array($trace['file'], $trace['line']), $linkFormat);
+ $content .= sprintf(' in <a href="%s" title="Go to source">%s line %s</a>', $link, $trace['file'], $trace['line']);
+ } else {
+ $content .= sprintf(' in %s line %s', $trace['file'], $trace['line']);
+ }
+ }
+ $content .= "</li>\n";
+ }
- if ($this->debug) {
- $content = $this->getContent($exception);
+ $content .= " </ol>\n</div>\n";
}
} catch (\Exception $e) {
- // something nasty happened and we cannot throw an exception here anymore
+ // something nasty happened and we cannot throw an exception 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), $exception->getStatusCode(), $exception->getHeaders());
- }
-
- 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 .= sprintf(<<<EOF
-<div class="block_exception clear_fix">
- <h2><span>%d/%d</span> %s: %s</h2>
-</div>
-<div class="block">
- <ol class="traces list_exception">
-
-EOF
- , $ind, $total, $class, $message);
- foreach ($e['trace'] as $i => $trace) {
- $content .= ' <li>';
- if ($trace['function']) {
- $content .= sprintf('at %s%s%s(%s)', $this->abbrClass($trace['class']), $trace['type'], $trace['function'], $this->formatArgs($trace['args']));
- }
- if (isset($trace['file']) && isset($trace['line'])) {
- if ($linkFormat = ini_get('xdebug.file_link_format')) {
- $link = str_replace(array('%f', '%l'), array($trace['file'], $trace['line']), $linkFormat);
- $content .= sprintf(' in <a href="%s" title="Go to source">%s line %s</a>', $link, $trace['file'], $trace['line']);
- } else {
- $content .= sprintf(' in %s line %s', $trace['file'], $trace['line']);
- }
- }
- $content .= "</li>\n";
- }
-
- $content .= " </ol>\n</div>\n";
- }
-
- return $content;
+ return <<<EOF
+ <div id="sf-exceptionresetcontent" class="sf-exceptionreset">
+ <h1>$title</h1>
+ $content
+ </div>
+EOF;
}
- private function decorate($content, $title)
+ /**
+ * Gets the stylesheet associated with the given exception.
+ *
+ * @param FlattenException $exception A FlattenException instance
+ *
+ * @return string The stylesheet as a string
+ */
+ public function getStylesheet(FlattenException $exception)
{
return <<<EOF
-<!DOCTYPE html>
-<html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
- <meta name="robots" content="noindex,nofollow" />
- <title>{$title}</title>
- <style>
- /* Copyright (c) 2010, Yahoo! Inc. All rights reserved. Code licensed under the BSD License: http://developer.yahoo.com/yui/license.html */
- html{color:#000;background:#FFF;}body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,textarea,p,blockquote,th,td{margin:0;padding:0;}table{border-collapse:collapse;border-spacing:0;}fieldset,img{border:0;}address,caption,cite,code,dfn,em,strong,th,var{font-style:normal;font-weight:normal;}li{list-style:none;}caption,th{text-align:left;}h1,h2,h3,h4,h5,h6{font-size:100%;font-weight:normal;}q:before,q:after{content:'';}abbr,acronym{border:0;font-variant:normal;}sup{vertical-align:text-top;}sub{vertical-align:text-bottom;}input,textarea,select{font-family:inherit;font-size:inherit;font-weight:inherit;}input,textarea,select{*font-size:100%;}legend{color:#000;}
-
- html { background: #eee; padding: 10px }
- body { font: 11px Verdana, Arial, sans-serif; color: #333 }
- img { border: 0; }
- .clear { clear:both; height:0; font-size:0; line-height:0; }
- .clear_fix:after { display:block; height:0; clear:both; visibility:hidden; }
- .clear_fix { display:inline-block; }
- * html .clear_fix { height:1%; }
- .clear_fix { display:block; }
- #content { width:970px; margin:0 auto; }
+ .sf-exceptionreset { font: 11px Verdana, Arial, sans-serif; color: #333 }
+ .sf-exceptionreset .clear { clear:both; height:0; font-size:0; line-height:0; }
+ .sf-exceptionreset .clear_fix:after { display:block; height:0; clear:both; visibility:hidden; }
+ .sf-exceptionreset .clear_fix { display:inline-block; }
+ .sf-exceptionreset * html .clear_fix { height:1%; }
+ .sf-exceptionreset .clear_fix { display:block; }
.sf-exceptionreset, .sf-exceptionreset .block { margin: auto }
.sf-exceptionreset abbr { border-bottom: 1px dotted #000; cursor: help; }
.sf-exceptionreset p { font-size:14px; line-height:20px; color:#868686; padding-bottom:20px }
@@ -209,13 +214,29 @@ private function decorate($content, $title)
border-radius: 10px;
border: 1px solid #ccc;
}
+EOF;
+ }
+
+ private function decorate($content, $css)
+ {
+ return <<<EOF
+<!DOCTYPE html>
+<html>
+ <head>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
+ <meta name="robots" content="noindex,nofollow" />
+ <style>
+ /* Copyright (c) 2010, Yahoo! Inc. All rights reserved. Code licensed under the BSD License: http://developer.yahoo.com/yui/license.html */
+ html{color:#000;background:#FFF;}body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,textarea,p,blockquote,th,td{margin:0;padding:0;}table{border-collapse:collapse;border-spacing:0;}fieldset,img{border:0;}address,caption,cite,code,dfn,em,strong,th,var{font-style:normal;font-weight:normal;}li{list-style:none;}caption,th{text-align:left;}h1,h2,h3,h4,h5,h6{font-size:100%;font-weight:normal;}q:before,q:after{content:'';}abbr,acronym{border:0;font-variant:normal;}sup{vertical-align:text-top;}sub{vertical-align:text-bottom;}input,textarea,select{font-family:inherit;font-size:inherit;font-weight:inherit;}input,textarea,select{*font-size:100%;}legend{color:#000;}
+
+ html { background: #eee; padding: 10px }
+ img { border: 0; }
+ #sf-exceptionresetcontent { width:970px; margin:0 auto; }
+ $css
</style>
</head>
<body>
- <div id="content" class="sf-exceptionreset">
- <h1>$title</h1>
-$content
- </div>
+ $content
</body>
</html>
EOF;
@@ -235,7 +256,7 @@ private function abbrClass($class)
*
* @return string
*/
- public function formatArgs(array $args)
+ private function formatArgs(array $args)
{
$result = array();
foreach ($args as $key => $item) {

0 comments on commit fbf19f0

Please sign in to comment.