From 761a9f44725358bbc4f08dad126065d9ed8413ab Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Fri, 1 Feb 2013 07:53:32 -0500 Subject: [PATCH] exception cleanup. --- framework/base/Application.php | 21 +++++++++++++++++-- framework/base/ErrorHandler.php | 31 +++++++++++++++-------------- framework/base/Exception.php | 12 +++++++++-- framework/base/HttpException.php | 4 ++++ framework/base/InvalidCallException.php | 7 +++++-- framework/base/InvalidConfigException.php | 7 +++++-- framework/base/InvalidRequestException.php | 12 +++++++++-- framework/base/InvalidRouteException.php | 12 +++++++++-- framework/base/NotSupportedException.php | 7 +++++-- framework/base/UnknownMethodException.php | 7 +++++-- framework/base/UnknownPropertyException.php | 7 +++++-- framework/console/Application.php | 2 +- framework/db/Exception.php | 13 +++++++----- 13 files changed, 103 insertions(+), 39 deletions(-) diff --git a/framework/base/Application.php b/framework/base/Application.php index e797ab7..f64e352 100644 --- a/framework/base/Application.php +++ b/framework/base/Application.php @@ -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 : '
' . $message . '
'; + $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 '
' . htmlspecialchars($message, ENT_QUOTES, $this->charset) . '
'; + } + } + // todo: to be polished protected function logException($exception) { diff --git a/framework/base/ErrorHandler.php b/framework/base/ErrorHandler.php index e544a49..0b6bf97 100644 --- a/framework/base/ErrorHandler.php +++ b/framework/base/ErrorHandler.php @@ -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, diff --git a/framework/base/Exception.php b/framework/base/Exception.php index 89fa30a..ab681e2 100644 --- a/framework/base/Exception.php +++ b/framework/base/Exception.php @@ -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'); + } } diff --git a/framework/base/HttpException.php b/framework/base/HttpException.php index 52ac690..d2de5bc 100644 --- a/framework/base/HttpException.php +++ b/framework/base/HttpException.php @@ -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. diff --git a/framework/base/InvalidCallException.php b/framework/base/InvalidCallException.php index e9d81b4..a1df021 100644 --- a/framework/base/InvalidCallException.php +++ b/framework/base/InvalidCallException.php @@ -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'); + } } diff --git a/framework/base/InvalidConfigException.php b/framework/base/InvalidConfigException.php index 8f292df..3c100d1 100644 --- a/framework/base/InvalidConfigException.php +++ b/framework/base/InvalidConfigException.php @@ -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'); + } } diff --git a/framework/base/InvalidRequestException.php b/framework/base/InvalidRequestException.php index 38c4115..fd468a1 100644 --- a/framework/base/InvalidRequestException.php +++ b/framework/base/InvalidRequestException.php @@ -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'); + } } diff --git a/framework/base/InvalidRouteException.php b/framework/base/InvalidRouteException.php index 6549c07..e20b2b7 100644 --- a/framework/base/InvalidRouteException.php +++ b/framework/base/InvalidRouteException.php @@ -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'); + } } diff --git a/framework/base/NotSupportedException.php b/framework/base/NotSupportedException.php index 2da008c..56e7e36 100644 --- a/framework/base/NotSupportedException.php +++ b/framework/base/NotSupportedException.php @@ -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'); + } } diff --git a/framework/base/UnknownMethodException.php b/framework/base/UnknownMethodException.php index b88d97f..459f791 100644 --- a/framework/base/UnknownMethodException.php +++ b/framework/base/UnknownMethodException.php @@ -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'); + } } diff --git a/framework/base/UnknownPropertyException.php b/framework/base/UnknownPropertyException.php index 601ae28..de8de1c 100644 --- a/framework/base/UnknownPropertyException.php +++ b/framework/base/UnknownPropertyException.php @@ -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'); + } } diff --git a/framework/console/Application.php b/framework/console/Application.php index 64c17e5..237be05 100644 --- a/framework/console/Application.php +++ b/framework/console/Application.php @@ -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; } } diff --git a/framework/db/Exception.php b/framework/db/Exception.php index 61a8e58..209dc40 100644 --- a/framework/db/Exception.php +++ b/framework/db/Exception.php @@ -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