Commit 761a9f44 by Qiang Xue

exception cleanup.

parent 6fcac324
......@@ -433,8 +433,7 @@ class Application extends Module
if (($handler = $this->getErrorHandler()) !== null) {
$handler->handle($exception);
} else {
$message = YII_DEBUG ? (string)$exception : 'Error: ' . $exception->getMessage() . "\n";
echo PHP_SAPI === 'cli' ? $message : '<pre>' . $message . '</pre>';
$this->renderException($exception);
}
$this->end(1);
......@@ -450,6 +449,24 @@ class Application extends Module
}
}
/**
* Renders an exception without using rich format.
* @param \Exception $exception the exception to be rendered.
*/
public function renderException($exception)
{
if ($exception instanceof Exception && ($exception->causedByUser || !YII_DEBUG)) {
$message = $exception->getName() . ': ' . $exception->getMessage();
} else {
$message = YII_DEBUG ? (string)$exception : 'Error: ' . $exception->getMessage();
}
if (PHP_SAPI) {
echo $message . "\n";
} else {
echo '<pre>' . htmlspecialchars($message, ENT_QUOTES, $this->charset) . '</pre>';
}
}
// todo: to be polished
protected function logException($exception)
{
......
......@@ -78,12 +78,20 @@ class ErrorHandler extends Component
header("HTTP/1.0 $errorCode " . get_class($exception));
}
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest') {
$this->renderAsText($exception);
\Yii::$application->renderException($exception);
} else {
$this->renderAsHtml($exception);
$view = new View($this);
if (!YII_DEBUG || $exception instanceof Exception && $exception->causedByUser) {
$viewName = $this->errorView;
} else {
$viewName = $this->exceptionView;
}
echo $view->render($viewName, array(
'exception' => $exception,
));
}
} else {
$this->renderAsText($exception);
\Yii::$application->renderException($exception);
}
}
......@@ -245,21 +253,14 @@ class ErrorHandler extends Component
/**
* @param \Exception $exception
*/
public function renderAsText($exception)
{
if (YII_DEBUG) {
echo $exception;
} else {
echo get_class($exception) . ': ' . $exception->getMessage();
}
}
/**
* @param \Exception $exception
*/
public function renderAsHtml($exception)
{
$view = new View($this);
if (!YII_DEBUG || $exception instanceof Exception && $exception->causedByUser) {
$viewName = $this->errorView;
} else {
$viewName = $this->exceptionView;
}
$name = !YII_DEBUG || $exception instanceof HttpException ? $this->errorView : $this->exceptionView;
echo $view->render($name, array(
'exception' => $exception,
......
......@@ -18,8 +18,16 @@ namespace yii\base;
class Exception extends \Exception
{
/**
* @var string the user-friend name of this exception
* @var boolean whether this exception is caused by end user's mistake (e.g. wrong URL)
*/
public $name = 'Exception';
public $causedByUser = false;
/**
* @return string the user-friendly name of this exception
*/
public function getName()
{
return \Yii::t('yii', 'Exception');
}
}
......@@ -25,6 +25,10 @@ class HttpException extends Exception
* @var integer HTTP status code, such as 403, 404, 500, etc.
*/
public $statusCode;
/**
* @var boolean whether this exception is caused by end user's mistake (e.g. wrong URL)
*/
public $causedByUser = true;
/**
* Constructor.
......
......@@ -18,8 +18,11 @@ namespace yii\base;
class InvalidCallException extends \Exception
{
/**
* @var string the user-friend name of this exception
* @return string the user-friendly name of this exception
*/
public $name = 'Invalid Call Exception';
public function getName()
{
return \Yii::t('yii', 'Invalid Call');
}
}
......@@ -18,8 +18,11 @@ namespace yii\base;
class InvalidConfigException extends \Exception
{
/**
* @var string the user-friend name of this exception
* @return string the user-friendly name of this exception
*/
public $name = 'Invalid Configuration Exception';
public function getName()
{
return \Yii::t('yii', 'Invalid Configuration');
}
}
......@@ -18,8 +18,16 @@ namespace yii\base;
class InvalidRequestException extends \Exception
{
/**
* @var string the user-friend name of this exception
* @var boolean whether this exception is caused by end user's mistake (e.g. wrong URL)
*/
public $name = 'Invalid Request Exception';
public $causedByUser = true;
/**
* @return string the user-friendly name of this exception
*/
public function getName()
{
return \Yii::t('yii', 'Invalid Request');
}
}
......@@ -18,8 +18,16 @@ namespace yii\base;
class InvalidRouteException extends \Exception
{
/**
* @var string the user-friend name of this exception
* @var boolean whether this exception is caused by end user's mistake (e.g. wrong URL)
*/
public $name = 'Invalid Route Exception';
public $causedByUser = true;
/**
* @return string the user-friendly name of this exception
*/
public function getName()
{
return \Yii::t('yii', 'Invalid Route');
}
}
......@@ -18,8 +18,11 @@ namespace yii\base;
class NotSupportedException extends \Exception
{
/**
* @var string the user-friend name of this exception
* @return string the user-friendly name of this exception
*/
public $name = 'Not Supported Exception';
public function getName()
{
return \Yii::t('yii', 'Not Supported');
}
}
......@@ -18,8 +18,11 @@ namespace yii\base;
class UnknownMethodException extends \Exception
{
/**
* @var string the user-friend name of this exception
* @return string the user-friendly name of this exception
*/
public $name = 'Unknown Method Exception';
public function getName()
{
return \Yii::t('yii', 'Unknown Method');
}
}
......@@ -18,8 +18,11 @@ namespace yii\base;
class UnknownPropertyException extends \Exception
{
/**
* @var string the user-friend name of this exception
* @return string the user-friendly name of this exception
*/
public $name = 'Unknown Property Exception';
public function getName()
{
return \Yii::t('yii', 'Unknown Property');
}
}
......@@ -112,7 +112,7 @@ class Application extends \yii\base\Application
try {
return parent::runAction($route, $params);
} catch (InvalidRouteException $e) {
echo "\nError: unknown command \"$route\".\n";
echo "Error: unknown command \"$route\".\n";
return 1;
}
}
......
......@@ -18,11 +18,6 @@ namespace yii\db;
class Exception extends \yii\base\Exception
{
/**
* @var string the user-friend name of this exception
*/
public $name = 'Database Exception';
/**
* @var mixed the error info provided by a PDO exception. This is the same as returned
* by [PDO::errorInfo](http://www.php.net/manual/en/pdo.errorinfo.php).
*/
......@@ -39,4 +34,12 @@ class Exception extends \yii\base\Exception
$this->errorInfo = $errorInfo;
parent::__construct($message, $code);
}
/**
* @return string the user-friendly name of this exception
*/
public function getName()
{
return \Yii::t('yii', 'Database Exception');
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment