Yii2 Bootstrap 3
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.

121 lines
3.2 KiB

13 years ago
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
13 years ago
* @license http://www.yiiframework.com/license/
*/
namespace yii\base;
13 years ago
use Yii;
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>
* @since 2.0
13 years ago
*/
class ErrorHandler extends Component
13 years ago
{
/**
* @var integer maximum number of source code lines to be displayed. Defaults to 25.
*/
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.
*/
public $discardExistingOutput = true;
13 years ago
/**
* @var string the route (e.g. 'site/error') to the controller action that will be used
* to display external errors. Inside the action, it can retrieve the error information
* by Yii::$app->errorHandler->error. 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 and errors.
13 years ago
*/
public $view = '@yii/views/errorHandler.php';
/**
* @var \Exception the exception that is being handled currently.
*/
public $exception;
12 years ago
13 years ago
/**
* Handles exception.
* @param \Exception $exception to be handled.
13 years ago
*/
public function handle($exception)
13 years ago
{
$this->exception = $exception;
if ($this->discardExistingOutput) {
$this->clearOutput();
}
$this->renderException($exception);
13 years ago
}
12 years ago
/**
* Renders exception.
* @param \Exception $exception to be handled.
12 years ago
*/
protected function renderException($exception)
13 years ago
{
13 years ago
if ($this->errorAction !== null) {
Yii::$app->runAction($this->errorAction);
} elseif (!(Yii::$app instanceof \yii\web\Application)) {
Yii::$app->renderException($exception);
} else {
13 years ago
if (!headers_sent()) {
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') {
Yii::$app->renderException($exception);
} else {
// 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) {
ini_set('display_errors', 1);
}
$view = new View();
echo $view->renderFile($this->view, array('e' => $exception), $this);
13 years ago
}
}
}
/**
* Converts special characters to HTML entities.
* @param string $text to encode.
* @return string encoded text.
13 years ago
*/
public function htmlEncode($text)
13 years ago
{
return htmlspecialchars($text, ENT_QUOTES, Yii::$app->charset);
}
12 years ago
/**
* Removes all output echoed before calling this method.
12 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
}