Browse Source

Fixes for issue #536: refactored error handler.

tags/2.0.0-beta
Qiang Xue 11 years ago
parent
commit
f3cc5d1edc
  1. 48
      framework/yii/base/ErrorHandler.php
  2. 12
      framework/yii/views/errorHandler/callStackItem.php
  3. 7
      framework/yii/views/errorHandler/error.php
  4. 38
      framework/yii/views/errorHandler/exception.php
  5. 15
      framework/yii/views/errorHandler/previousException.php

48
framework/yii/base/ErrorHandler.php

@ -110,11 +110,10 @@ class ErrorHandler extends Component
ini_set('display_errors', 1); ini_set('display_errors', 1);
} }
$view = new View();
$file = $useErrorView ? $this->errorView : $this->exceptionView; $file = $useErrorView ? $this->errorView : $this->exceptionView;
$response->content = $view->renderFile($file, array( $response->content = $this->renderFile($file, array(
'exception' => $exception, 'exception' => $exception,
), $this); ));
} }
} }
@ -176,14 +175,18 @@ class ErrorHandler extends Component
} }
/** /**
* Creates HTML containing link to the page with the information on given HTTP status code. * Renders a view file as a PHP script.
* @param integer $statusCode to be used to generate information link. * @param string $_file_ the view file.
* @param string $statusDescription Description to display after the the status code. * @param array $_params_ the parameters (name-value pairs) that will be extracted and made available in the view file.
* @return string generated HTML with HTTP status code information. * @return string the rendering result
*/ */
public function createHttpStatusLink($statusCode, $statusDescription) public function renderFile($_file_, $_params_)
{ {
return '<a href="http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#' . (int)$statusCode .'" target="_blank">HTTP ' . (int)$statusCode . ' &ndash; ' . $statusDescription . '</a>'; ob_start();
ob_implicit_flush(false);
extract($_params_, EXTR_OVERWRITE);
require(Yii::getAlias($_file_));
return ob_get_clean();
} }
/** /**
@ -194,14 +197,13 @@ class ErrorHandler extends Component
*/ */
public function renderPreviousExceptions($exception) public function renderPreviousExceptions($exception)
{ {
if (($previous = $exception->getPrevious()) === null) { if (($previous = $exception->getPrevious()) !== null) {
return $this->renderFile($this->previousExceptionView, array(
'exception' => $previous,
));
} else {
return ''; return '';
} }
$view = new View();
return $view->renderFile($this->previousExceptionView, array(
'exception' => $previous,
'previousHtml' => $this->renderPreviousExceptions($previous),
), $this);
} }
/** /**
@ -229,8 +231,7 @@ class ErrorHandler extends Component
$end = $line + $half < $lineCount ? $line + $half : $lineCount - 1; $end = $line + $half < $lineCount ? $line + $half : $lineCount - 1;
} }
$view = new View(); return $this->renderFile($this->callStackItemView, array(
return $view->renderFile($this->callStackItemView, array(
'file' => $file, 'file' => $file,
'line' => $line, 'line' => $line,
'class' => $class, 'class' => $class,
@ -239,7 +240,7 @@ class ErrorHandler extends Component
'lines' => $lines, 'lines' => $lines,
'begin' => $begin, 'begin' => $begin,
'end' => $end, 'end' => $end,
), $this); ));
} }
/** /**
@ -268,6 +269,17 @@ class ErrorHandler extends Component
} }
/** /**
* Creates HTML containing link to the page with the information on given HTTP status code.
* @param integer $statusCode to be used to generate information link.
* @param string $statusDescription Description to display after the the status code.
* @return string generated HTML with HTTP status code information.
*/
public function createHttpStatusLink($statusCode, $statusDescription)
{
return '<a href="http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#' . (int)$statusCode .'" target="_blank">HTTP ' . (int)$statusCode . ' &ndash; ' . $statusDescription . '</a>';
}
/**
* Creates string containing HTML link which refers to the home page of determined web-server software * Creates string containing HTML link which refers to the home page of determined web-server software
* and its full name. * and its full name.
* @return string server software information hyperlink. * @return string server software information hyperlink.

12
framework/yii/views/errorHandler/callStackItem.php

@ -1,6 +1,5 @@
<?php <?php
/** /**
* @var \yii\base\View $this
* @var string|null $file * @var string|null $file
* @var integer|null $line * @var integer|null $line
* @var string|null $class * @var string|null $class
@ -9,20 +8,19 @@
* @var string[] $lines * @var string[] $lines
* @var integer $begin * @var integer $begin
* @var integer $end * @var integer $end
* @var \yii\base\ErrorHandler $context * @var \yii\base\ErrorHandler $this
*/ */
$context = $this->context;
?> ?>
<li class="<?php if (!$context->isCoreFile($file) || $index === 1) echo 'application'; ?> call-stack-item" <li class="<?php if (!$this->isCoreFile($file) || $index === 1) echo 'application'; ?> call-stack-item"
data-line="<?php echo (int)($line - $begin); ?>"> data-line="<?php echo (int)($line - $begin); ?>">
<div class="element-wrap"> <div class="element-wrap">
<div class="element"> <div class="element">
<span class="item-number"><?php echo (int)$index; ?>.</span> <span class="item-number"><?php echo (int)$index; ?>.</span>
<span class="text"><?php if ($file !== null) echo 'in ' . $context->htmlEncode($file); ?></span> <span class="text"><?php if ($file !== null) echo 'in ' . $this->htmlEncode($file); ?></span>
<?php if ($method !== null): ?> <?php if ($method !== null): ?>
<span class="call"> <span class="call">
<?php if ($file !== null) echo '&ndash;' ?> <?php if ($file !== null) echo '&ndash;' ?>
<?php if ($class !== null) echo $context->addTypeLinks($class) . '→'; ?><?php echo $context->addTypeLinks($method . '()'); ?> <?php if ($class !== null) echo $this->addTypeLinks($class) . '→'; ?><?php echo $this->addTypeLinks($method . '()'); ?>
</span> </span>
<?php endif; ?> <?php endif; ?>
<span class="at"><?php if ($line !== null) echo 'at line'; ?></span> <span class="at"><?php if ($line !== null) echo 'at line'; ?></span>
@ -38,7 +36,7 @@ $context = $this->context;
<pre><?php <pre><?php
// fill empty lines with a whitespace to avoid rendering problems in opera // fill empty lines with a whitespace to avoid rendering problems in opera
for ($i = $begin; $i <= $end; ++$i) { for ($i = $begin; $i <= $end; ++$i) {
echo (trim($lines[$i]) == '') ? " \n" : $context->htmlEncode($lines[$i]); echo (trim($lines[$i]) == '') ? " \n" : $this->htmlEncode($lines[$i]);
} }
?></pre> ?></pre>
</div> </div>

7
framework/yii/views/errorHandler/error.php

@ -1,10 +1,9 @@
<?php <?php
/** /**
* @var \Exception $exception * @var \Exception $exception
* @var \yii\base\ErrorHandler $context * @var \yii\base\ErrorHandler $this
*/ */
$context = $this->context; $title = $this->htmlEncode($exception instanceof \yii\base\Exception ? $exception->getName() : get_class($exception));
$title = $context->htmlEncode($exception instanceof \yii\base\Exception ? $exception->getName() : get_class($exception));
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
@ -52,7 +51,7 @@ $title = $context->htmlEncode($exception instanceof \yii\base\Exception ? $excep
<body> <body>
<h1><?php echo $title?></h1> <h1><?php echo $title?></h1>
<h2><?php echo nl2br($context->htmlEncode($exception->getMessage()))?></h2> <h2><?php echo nl2br($this->htmlEncode($exception->getMessage()))?></h2>
<p> <p>
The above error occurred while the Web server was processing your request. The above error occurred while the Web server was processing your request.
</p> </p>

38
framework/yii/views/errorHandler/exception.php

File diff suppressed because one or more lines are too long

15
framework/yii/views/errorHandler/previousException.php

@ -1,24 +1,21 @@
<?php <?php
/** /**
* @var \yii\base\View $this
* @var \yii\base\Exception $exception * @var \yii\base\Exception $exception
* @var string $previousHtml * @var \yii\base\ErrorHandler $this
* @var \yii\base\ErrorHandler $context
*/ */
$context = $this->context;
?> ?>
<div class="previous"> <div class="previous">
<span class="arrow">&crarr;</span> <span class="arrow">&crarr;</span>
<h2> <h2>
<span>Caused by:</span> <span>Caused by:</span>
<?php if ($exception instanceof \yii\base\Exception): ?> <?php if ($exception instanceof \yii\base\Exception): ?>
<span><?php echo $context->htmlEncode($exception->getName()); ?></span> &ndash; <span><?php echo $this->htmlEncode($exception->getName()); ?></span> &ndash;
<?php echo $context->addTypeLinks(get_class($exception)); ?> <?php echo $this->addTypeLinks(get_class($exception)); ?>
<?php else: ?> <?php else: ?>
<span><?php echo $context->htmlEncode(get_class($exception)); ?></span> <span><?php echo $this->htmlEncode(get_class($exception)); ?></span>
<?php endif; ?> <?php endif; ?>
</h2> </h2>
<h3><?php echo $context->htmlEncode($exception->getMessage()); ?></h3> <h3><?php echo $this->htmlEncode($exception->getMessage()); ?></h3>
<p>in <span class="file"><?php echo $exception->getFile(); ?></span> at line <span class="line"><?php echo $exception->getLine(); ?></span></p> <p>in <span class="file"><?php echo $exception->getFile(); ?></span> at line <span class="line"><?php echo $exception->getLine(); ?></span></p>
<?php echo $previousHtml; ?> <?php echo $this->renderPreviousExceptions($exception); ?>
</div> </div>

Loading…
Cancel
Save