You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
284 lines
7.9 KiB
284 lines
7.9 KiB
13 years ago
|
<?php
|
||
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
12 years ago
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
13 years ago
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
13 years ago
|
namespace yii\base;
|
||
13 years ago
|
|
||
|
/**
|
||
13 years ago
|
* ErrorHandler handles uncaught PHP errors and exceptions.
|
||
13 years ago
|
*
|
||
13 years ago
|
* ErrorHandler displays these errors using appropriate views based on the
|
||
|
* nature of the errors and the mode the application runs at.
|
||
|
*
|
||
13 years ago
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||
13 years ago
|
* @since 2.0
|
||
13 years ago
|
*/
|
||
12 years ago
|
class ErrorHandler extends Component
|
||
13 years ago
|
{
|
||
|
/**
|
||
|
* @var integer maximum number of source code lines to be displayed. Defaults to 25.
|
||
|
*/
|
||
13 years ago
|
public $maxSourceLines = 25;
|
||
13 years ago
|
/**
|
||
|
* @var integer maximum number of trace source code lines to be displayed. Defaults to 10.
|
||
|
*/
|
||
|
public $maxTraceSourceLines = 10;
|
||
|
/**
|
||
|
* @var boolean whether to discard any existing page output before error display. Defaults to true.
|
||
|
*/
|
||
13 years ago
|
public $discardExistingOutput = true;
|
||
13 years ago
|
/**
|
||
|
* @var string the route (eg 'site/error') to the controller action that will be used to display external errors.
|
||
12 years ago
|
* Inside the action, it can retrieve the error information by \Yii::$app->errorHandler->error.
|
||
13 years ago
|
* This property defaults to null, meaning ErrorHandler will handle the error display.
|
||
13 years ago
|
*/
|
||
|
public $errorAction;
|
||
13 years ago
|
/**
|
||
|
* @var string the path of the view file for rendering exceptions
|
||
|
*/
|
||
13 years ago
|
public $exceptionView = '@yii/views/exception.php';
|
||
13 years ago
|
/**
|
||
|
* @var string the path of the view file for rendering errors
|
||
|
*/
|
||
13 years ago
|
public $errorView = '@yii/views/error.php';
|
||
|
/**
|
||
|
* @var \Exception the exception that is being handled currently
|
||
|
*/
|
||
13 years ago
|
public $exception;
|
||
12 years ago
|
|
||
13 years ago
|
|
||
13 years ago
|
/**
|
||
12 years ago
|
* Handles exception
|
||
13 years ago
|
* @param \Exception $exception
|
||
13 years ago
|
*/
|
||
12 years ago
|
public function handle($exception)
|
||
13 years ago
|
{
|
||
13 years ago
|
$this->exception = $exception;
|
||
|
|
||
13 years ago
|
if ($this->discardExistingOutput) {
|
||
|
$this->clearOutput();
|
||
|
}
|
||
|
|
||
12 years ago
|
$this->renderException($exception);
|
||
13 years ago
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
|
* Renders exception
|
||
|
* @param \Exception $exception
|
||
|
*/
|
||
12 years ago
|
protected function renderException($exception)
|
||
13 years ago
|
{
|
||
13 years ago
|
if ($this->errorAction !== null) {
|
||
12 years ago
|
\Yii::$app->runAction($this->errorAction);
|
||
|
} elseif (\Yii::$app instanceof \yii\web\Application) {
|
||
13 years ago
|
if (!headers_sent()) {
|
||
12 years ago
|
if ($exception instanceof HttpException) {
|
||
|
header('HTTP/1.0 ' . $exception->statusCode . ' ' . $exception->getName());
|
||
|
} else {
|
||
|
header('HTTP/1.0 500 ' . get_class($exception));
|
||
|
}
|
||
13 years ago
|
}
|
||
|
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest') {
|
||
12 years ago
|
\Yii::$app->renderException($exception);
|
||
13 years ago
|
} else {
|
||
12 years ago
|
// if there is an error during error rendering it's useful to
|
||
|
// display PHP error in debug mode instead of a blank screen
|
||
12 years ago
|
if (YII_DEBUG) {
|
||
12 years ago
|
ini_set('display_errors', 1);
|
||
12 years ago
|
}
|
||
12 years ago
|
|
||
|
$view = new View;
|
||
|
if (!YII_DEBUG || $exception instanceof UserException) {
|
||
|
$viewName = $this->errorView;
|
||
|
} else {
|
||
|
$viewName = $this->exceptionView;
|
||
|
}
|
||
|
echo $view->renderFile($viewName, array(
|
||
|
'exception' => $exception,
|
||
|
), $this);
|
||
13 years ago
|
}
|
||
13 years ago
|
} else {
|
||
12 years ago
|
\Yii::$app->renderException($exception);
|
||
13 years ago
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
13 years ago
|
* Returns server and Yii version information.
|
||
|
* @return string server version information.
|
||
13 years ago
|
*/
|
||
13 years ago
|
public function getVersionInfo()
|
||
13 years ago
|
{
|
||
13 years ago
|
$version = '<a href="http://www.yiiframework.com/">Yii Framework</a>/' . \Yii::getVersion();
|
||
|
if (isset($_SERVER['SERVER_SOFTWARE'])) {
|
||
|
$version = $_SERVER['SERVER_SOFTWARE'] . ' ' . $version;
|
||
13 years ago
|
}
|
||
|
return $version;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Converts arguments array to its string representation
|
||
|
*
|
||
|
* @param array $args arguments array to be converted
|
||
|
* @return string string representation of the arguments array
|
||
|
*/
|
||
13 years ago
|
public function argumentsToString($args)
|
||
13 years ago
|
{
|
||
13 years ago
|
$isAssoc = $args !== array_values($args);
|
||
13 years ago
|
$count = 0;
|
||
|
foreach ($args as $key => $value) {
|
||
13 years ago
|
$count++;
|
||
13 years ago
|
if ($count >= 5) {
|
||
13 years ago
|
if ($count > 5) {
|
||
13 years ago
|
unset($args[$key]);
|
||
13 years ago
|
} else {
|
||
13 years ago
|
$args[$key] = '...';
|
||
13 years ago
|
}
|
||
13 years ago
|
continue;
|
||
|
}
|
||
|
|
||
13 years ago
|
if (is_object($value)) {
|
||
13 years ago
|
$args[$key] = get_class($value);
|
||
13 years ago
|
} elseif (is_bool($value)) {
|
||
13 years ago
|
$args[$key] = $value ? 'true' : 'false';
|
||
13 years ago
|
} elseif (is_string($value)) {
|
||
|
if (strlen($value) > 64) {
|
||
13 years ago
|
$args[$key] = '"' . substr($value, 0, 64) . '..."';
|
||
13 years ago
|
} else {
|
||
13 years ago
|
$args[$key] = '"' . $value . '"';
|
||
13 years ago
|
}
|
||
|
} elseif (is_array($value)) {
|
||
13 years ago
|
$args[$key] = 'array(' . $this->argumentsToString($value) . ')';
|
||
13 years ago
|
} elseif ($value === null) {
|
||
13 years ago
|
$args[$key] = 'null';
|
||
13 years ago
|
} elseif (is_resource($value)) {
|
||
13 years ago
|
$args[$key] = 'resource';
|
||
13 years ago
|
}
|
||
13 years ago
|
|
||
13 years ago
|
if (is_string($key)) {
|
||
|
$args[$key] = '"' . $key . '" => ' . $args[$key];
|
||
13 years ago
|
} elseif ($isAssoc) {
|
||
13 years ago
|
$args[$key] = $key . ' => ' . $args[$key];
|
||
13 years ago
|
}
|
||
|
}
|
||
13 years ago
|
return implode(', ', $args);
|
||
13 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns a value indicating whether the call stack is from application code.
|
||
|
* @param array $trace the trace data
|
||
|
* @return boolean whether the call stack is from application code.
|
||
|
*/
|
||
13 years ago
|
public function isCoreCode($trace)
|
||
13 years ago
|
{
|
||
13 years ago
|
if (isset($trace['file'])) {
|
||
13 years ago
|
return $trace['file'] === 'unknown' || strpos(realpath($trace['file']), YII_PATH . DIRECTORY_SEPARATOR) === 0;
|
||
13 years ago
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Renders the source code around the error line.
|
||
|
* @param string $file source file path
|
||
|
* @param integer $errorLine the error line number
|
||
|
* @param integer $maxLines maximum number of lines to display
|
||
|
*/
|
||
13 years ago
|
public function renderSourceCode($file, $errorLine, $maxLines)
|
||
13 years ago
|
{
|
||
13 years ago
|
$errorLine--; // adjust line number to 0-based from 1-based
|
||
13 years ago
|
if ($errorLine < 0 || ($lines = @file($file)) === false || ($lineCount = count($lines)) <= $errorLine) {
|
||
|
return;
|
||
|
}
|
||
13 years ago
|
|
||
13 years ago
|
$halfLines = (int)($maxLines / 2);
|
||
|
$beginLine = $errorLine - $halfLines > 0 ? $errorLine - $halfLines : 0;
|
||
|
$endLine = $errorLine + $halfLines < $lineCount ? $errorLine + $halfLines : $lineCount - 1;
|
||
|
$lineNumberWidth = strlen($endLine + 1);
|
||
13 years ago
|
|
||
13 years ago
|
$output = '';
|
||
13 years ago
|
for ($i = $beginLine; $i <= $endLine; ++$i) {
|
||
13 years ago
|
$isErrorLine = $i === $errorLine;
|
||
13 years ago
|
$code = sprintf("<span class=\"ln" . ($isErrorLine ? ' error-ln' : '') . "\">%0{$lineNumberWidth}d</span> %s", $i + 1, $this->htmlEncode(str_replace("\t", ' ', $lines[$i])));
|
||
|
if (!$isErrorLine) {
|
||
13 years ago
|
$output .= $code;
|
||
13 years ago
|
} else {
|
||
13 years ago
|
$output .= '<span class="error">' . $code . '</span>';
|
||
13 years ago
|
}
|
||
|
}
|
||
|
echo '<div class="code"><pre>' . $output . '</pre></div>';
|
||
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
|
* Renders calls stack trace
|
||
|
* @param array $trace
|
||
|
*/
|
||
13 years ago
|
public function renderTrace($trace)
|
||
|
{
|
||
|
$count = 0;
|
||
|
echo "<table>\n";
|
||
|
foreach ($trace as $n => $t) {
|
||
|
if ($this->isCoreCode($t)) {
|
||
|
$cssClass = 'core collapsed';
|
||
|
} elseif (++$count > 3) {
|
||
|
$cssClass = 'app collapsed';
|
||
|
} else {
|
||
|
$cssClass = 'app expanded';
|
||
|
}
|
||
12 years ago
|
|
||
|
$hasCode = isset($t['file']) && $t['file'] !== 'unknown' && is_file($t['file']);
|
||
13 years ago
|
echo "<tr class=\"trace $cssClass\"><td class=\"number\">#$n</td><td class=\"content\">";
|
||
|
echo '<div class="trace-file">';
|
||
|
if ($hasCode) {
|
||
|
echo '<div class="plus">+</div><div class="minus">-</div>';
|
||
|
}
|
||
|
echo ' ';
|
||
12 years ago
|
if (isset($t['file'])) {
|
||
12 years ago
|
echo $this->htmlEncode($t['file']) . '(' . $t['line'] . '): ';
|
||
|
}
|
||
13 years ago
|
if (!empty($t['class'])) {
|
||
|
echo '<strong>' . $t['class'] . '</strong>' . $t['type'];
|
||
|
}
|
||
|
echo '<strong>' . $t['function'] . '</strong>';
|
||
|
echo '(' . (empty($t['args']) ? '' : $this->htmlEncode($this->argumentsToString($t['args']))) . ')';
|
||
|
echo '</div>';
|
||
|
if ($hasCode) {
|
||
|
$this->renderSourceCode($t['file'], $t['line'], $this->maxTraceSourceLines);
|
||
|
}
|
||
|
echo "</td></tr>\n";
|
||
|
}
|
||
|
echo '</table>';
|
||
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
|
* Converts special characters to HTML entities
|
||
|
* @param string $text text to encode
|
||
|
* @return string
|
||
|
*/
|
||
13 years ago
|
public function htmlEncode($text)
|
||
|
{
|
||
12 years ago
|
return htmlspecialchars($text, ENT_QUOTES, \Yii::$app->charset);
|
||
13 years ago
|
}
|
||
|
|
||
|
public function clearOutput()
|
||
|
{
|
||
|
// the following manual level counting is to deal with zlib.output_compression set to On
|
||
|
for ($level = ob_get_level(); $level > 0; --$level) {
|
||
|
@ob_end_clean();
|
||
13 years ago
|
}
|
||
13 years ago
|
}
|
||
|
|
||
|
/**
|
||
13 years ago
|
* @param \Exception $exception
|
||
13 years ago
|
*/
|
||
13 years ago
|
public function renderAsHtml($exception)
|
||
|
{
|
||
12 years ago
|
$view = new View;
|
||
13 years ago
|
$name = !YII_DEBUG || $exception instanceof HttpException ? $this->errorView : $this->exceptionView;
|
||
12 years ago
|
echo $view->renderFile($name, array(
|
||
13 years ago
|
'exception' => $exception,
|
||
12 years ago
|
), $this);
|
||
13 years ago
|
}
|
||
|
}
|