Commit 761a9f44 by Qiang Xue

exception cleanup.

parent 6fcac324
...@@ -433,8 +433,7 @@ class Application extends Module ...@@ -433,8 +433,7 @@ class Application extends Module
if (($handler = $this->getErrorHandler()) !== null) { if (($handler = $this->getErrorHandler()) !== null) {
$handler->handle($exception); $handler->handle($exception);
} else { } else {
$message = YII_DEBUG ? (string)$exception : 'Error: ' . $exception->getMessage() . "\n"; $this->renderException($exception);
echo PHP_SAPI === 'cli' ? $message : '<pre>' . $message . '</pre>';
} }
$this->end(1); $this->end(1);
...@@ -450,6 +449,24 @@ class Application extends Module ...@@ -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 // todo: to be polished
protected function logException($exception) protected function logException($exception)
{ {
......
...@@ -78,12 +78,20 @@ class ErrorHandler extends Component ...@@ -78,12 +78,20 @@ class ErrorHandler extends Component
header("HTTP/1.0 $errorCode " . get_class($exception)); header("HTTP/1.0 $errorCode " . get_class($exception));
} }
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest') { if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest') {
$this->renderAsText($exception); \Yii::$application->renderException($exception);
} else { } 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 { } else {
$this->renderAsText($exception); \Yii::$application->renderException($exception);
} }
} }
...@@ -245,21 +253,14 @@ class ErrorHandler extends Component ...@@ -245,21 +253,14 @@ class ErrorHandler extends Component
/** /**
* @param \Exception $exception * @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) public function renderAsHtml($exception)
{ {
$view = new View($this); $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; $name = !YII_DEBUG || $exception instanceof HttpException ? $this->errorView : $this->exceptionView;
echo $view->render($name, array( echo $view->render($name, array(
'exception' => $exception, 'exception' => $exception,
......
...@@ -18,8 +18,16 @@ namespace yii\base; ...@@ -18,8 +18,16 @@ namespace yii\base;
class Exception extends \Exception 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 ...@@ -25,6 +25,10 @@ class HttpException extends Exception
* @var integer HTTP status code, such as 403, 404, 500, etc. * @var integer HTTP status code, such as 403, 404, 500, etc.
*/ */
public $statusCode; public $statusCode;
/**
* @var boolean whether this exception is caused by end user's mistake (e.g. wrong URL)
*/
public $causedByUser = true;
/** /**
* Constructor. * Constructor.
......
...@@ -18,8 +18,11 @@ namespace yii\base; ...@@ -18,8 +18,11 @@ namespace yii\base;
class InvalidCallException extends \Exception 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; ...@@ -18,8 +18,11 @@ namespace yii\base;
class InvalidConfigException extends \Exception 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; ...@@ -18,8 +18,16 @@ namespace yii\base;
class InvalidRequestException extends \Exception 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; ...@@ -18,8 +18,16 @@ namespace yii\base;
class InvalidRouteException extends \Exception 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; ...@@ -18,8 +18,11 @@ namespace yii\base;
class NotSupportedException extends \Exception 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; ...@@ -18,8 +18,11 @@ namespace yii\base;
class UnknownMethodException extends \Exception 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; ...@@ -18,8 +18,11 @@ namespace yii\base;
class UnknownPropertyException extends \Exception 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 ...@@ -112,7 +112,7 @@ class Application extends \yii\base\Application
try { try {
return parent::runAction($route, $params); return parent::runAction($route, $params);
} catch (InvalidRouteException $e) { } catch (InvalidRouteException $e) {
echo "\nError: unknown command \"$route\".\n"; echo "Error: unknown command \"$route\".\n";
return 1; return 1;
} }
} }
......
...@@ -18,11 +18,6 @@ namespace yii\db; ...@@ -18,11 +18,6 @@ namespace yii\db;
class Exception extends \yii\base\Exception 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 * @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). * by [PDO::errorInfo](http://www.php.net/manual/en/pdo.errorinfo.php).
*/ */
...@@ -39,4 +34,12 @@ class Exception extends \yii\base\Exception ...@@ -39,4 +34,12 @@ class Exception extends \yii\base\Exception
$this->errorInfo = $errorInfo; $this->errorInfo = $errorInfo;
parent::__construct($message, $code); 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