From f3cc5d1edc46c59bf26d5adba325308ab1375e01 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Fri, 14 Jun 2013 08:19:45 -0400 Subject: [PATCH] Fixes for issue #536: refactored error handler. --- framework/yii/base/ErrorHandler.php | 48 ++++++++++++++-------- framework/yii/views/errorHandler/callStackItem.php | 12 +++--- framework/yii/views/errorHandler/error.php | 7 ++-- framework/yii/views/errorHandler/exception.php | 38 ++++++++--------- .../yii/views/errorHandler/previousException.php | 15 +++---- 5 files changed, 62 insertions(+), 58 deletions(-) diff --git a/framework/yii/base/ErrorHandler.php b/framework/yii/base/ErrorHandler.php index a6e282b..e9ca91e 100644 --- a/framework/yii/base/ErrorHandler.php +++ b/framework/yii/base/ErrorHandler.php @@ -110,11 +110,10 @@ class ErrorHandler extends Component ini_set('display_errors', 1); } - $view = new View(); $file = $useErrorView ? $this->errorView : $this->exceptionView; - $response->content = $view->renderFile($file, array( + $response->content = $this->renderFile($file, array( '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. - * @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. + * Renders a view file as a PHP script. + * @param string $_file_ the view file. + * @param array $_params_ the parameters (name-value pairs) that will be extracted and made available in the view file. + * @return string the rendering result */ - public function createHttpStatusLink($statusCode, $statusDescription) + public function renderFile($_file_, $_params_) { - return 'HTTP ' . (int)$statusCode . ' – ' . $statusDescription . ''; + 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) { - if (($previous = $exception->getPrevious()) === null) { + if (($previous = $exception->getPrevious()) !== null) { + return $this->renderFile($this->previousExceptionView, array( + 'exception' => $previous, + )); + } else { 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; } - $view = new View(); - return $view->renderFile($this->callStackItemView, array( + return $this->renderFile($this->callStackItemView, array( 'file' => $file, 'line' => $line, 'class' => $class, @@ -239,7 +240,7 @@ class ErrorHandler extends Component 'lines' => $lines, 'begin' => $begin, '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 'HTTP ' . (int)$statusCode . ' – ' . $statusDescription . ''; + } + + /** * Creates string containing HTML link which refers to the home page of determined web-server software * and its full name. * @return string server software information hyperlink. diff --git a/framework/yii/views/errorHandler/callStackItem.php b/framework/yii/views/errorHandler/callStackItem.php index 7514119..dc6f12d 100644 --- a/framework/yii/views/errorHandler/callStackItem.php +++ b/framework/yii/views/errorHandler/callStackItem.php @@ -1,6 +1,5 @@ context; ?> -
  • . - htmlEncode($file); ?> + htmlEncode($file); ?> - addTypeLinks($class) . '→'; ?>addTypeLinks($method . '()'); ?> + addTypeLinks($class) . '→'; ?>addTypeLinks($method . '()'); ?> @@ -38,7 +36,7 @@ $context = $this->context;
    htmlEncode($lines[$i]);
    +						echo (trim($lines[$i]) == '') ? " \n" : $this->htmlEncode($lines[$i]);
     					}
     				?>
    diff --git a/framework/yii/views/errorHandler/error.php b/framework/yii/views/errorHandler/error.php index fc5dbc7..4765bdd 100644 --- a/framework/yii/views/errorHandler/error.php +++ b/framework/yii/views/errorHandler/error.php @@ -1,10 +1,9 @@ context; -$title = $context->htmlEncode($exception instanceof \yii\base\Exception ? $exception->getName() : get_class($exception)); +$title = $this->htmlEncode($exception instanceof \yii\base\Exception ? $exception->getName() : get_class($exception)); ?> @@ -52,7 +51,7 @@ $title = $context->htmlEncode($exception instanceof \yii\base\Exception ? $excep

    -

    htmlEncode($exception->getMessage()))?>

    +

    htmlEncode($exception->getMessage()))?>

    The above error occurred while the Web server was processing your request.

    diff --git a/framework/yii/views/errorHandler/exception.php b/framework/yii/views/errorHandler/exception.php index b0c1f1c..8c6612a 100644 --- a/framework/yii/views/errorHandler/exception.php +++ b/framework/yii/views/errorHandler/exception.php @@ -1,10 +1,8 @@ context; ?> @@ -14,11 +12,11 @@ $context = $this->context; <?php if ($exception instanceof \yii\web\HttpException) { - echo (int) $exception->statusCode . ' ' . $context->htmlEncode($exception->getName()); + echo (int) $exception->statusCode . ' ' . $this->htmlEncode($exception->getName()); } elseif ($exception instanceof \yii\base\Exception) { - echo $context->htmlEncode($exception->getName() . ' – ' . get_class($exception)); + echo $this->htmlEncode($exception->getName() . ' – ' . get_class($exception)); } else { - echo $context->htmlEncode(get_class($exception)); + echo $this->htmlEncode(get_class($exception)); } ?> @@ -355,32 +353,32 @@ pre .diff .change{ Gears

    - htmlEncode($exception->getName()); ?> - – addTypeLinks(get_class($exception)); ?> + htmlEncode($exception->getName()); ?> + – addTypeLinks(get_class($exception)); ?>

    Attention

    ' . $context->createHttpStatusLink($exception->statusCode, $context->htmlEncode($exception->getName())) . ''; - echo ' – ' . $context->addTypeLinks(get_class($exception)); + echo '' . $this->createHttpStatusLink($exception->statusCode, $this->htmlEncode($exception->getName())) . ''; + echo ' – ' . $this->addTypeLinks(get_class($exception)); } elseif ($exception instanceof \yii\base\Exception) { - echo '' . $context->htmlEncode($exception->getName()) . ''; - echo ' – ' . $context->addTypeLinks(get_class($exception)); + echo '' . $this->htmlEncode($exception->getName()) . ''; + echo ' – ' . $this->addTypeLinks(get_class($exception)); } else { - echo '' . $context->htmlEncode(get_class($exception)) . ''; + echo '' . $this->htmlEncode(get_class($exception)) . ''; } ?>

    -

    htmlEncode($exception->getMessage()); ?>

    - renderPreviousExceptions($exception); ?> +

    htmlEncode($exception->getMessage()); ?>

    + renderPreviousExceptions($exception); ?>
      - renderCallStackItem($exception->getFile(), $exception->getLine(), null, null, 1); ?> + renderCallStackItem($exception->getFile(), $exception->getLine(), null, null, 1); ?> getTrace(), $length = count($trace); $i < $length; ++$i): ?> - renderCallStackItem(@$trace[$i]['file'] ?: null, @$trace[$i]['line'] ?: null, + renderCallStackItem(@$trace[$i]['file'] ?: null, @$trace[$i]['line'] ?: null, @$trace[$i]['class'] ?: null, @$trace[$i]['function'] ?: null, $i + 1); ?>
    @@ -388,15 +386,15 @@ pre .diff .change{
    - renderRequest(); ?> + renderRequest(); ?>