Browse Source

exception cleanup.

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
761a9f4472
  1. 21
      framework/base/Application.php
  2. 31
      framework/base/ErrorHandler.php
  3. 12
      framework/base/Exception.php
  4. 4
      framework/base/HttpException.php
  5. 7
      framework/base/InvalidCallException.php
  6. 7
      framework/base/InvalidConfigException.php
  7. 12
      framework/base/InvalidRequestException.php
  8. 12
      framework/base/InvalidRouteException.php
  9. 7
      framework/base/NotSupportedException.php
  10. 7
      framework/base/UnknownMethodException.php
  11. 7
      framework/base/UnknownPropertyException.php
  12. 2
      framework/console/Application.php
  13. 13
      framework/db/Exception.php

21
framework/base/Application.php

@ -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
} }
} }
/**
* 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)
{ {

31
framework/base/ErrorHandler.php

@ -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
/** /**
* @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,

12
framework/base/Exception.php

@ -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');
}
} }

4
framework/base/HttpException.php

@ -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.

7
framework/base/InvalidCallException.php

@ -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');
}
} }

7
framework/base/InvalidConfigException.php

@ -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');
}
} }

12
framework/base/InvalidRequestException.php

@ -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');
}
} }

12
framework/base/InvalidRouteException.php

@ -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');
}
} }

7
framework/base/NotSupportedException.php

@ -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');
}
} }

7
framework/base/UnknownMethodException.php

@ -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');
}
} }

7
framework/base/UnknownPropertyException.php

@ -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');
}
} }

2
framework/console/Application.php

@ -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;
} }
} }

13
framework/db/Exception.php

@ -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
$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');
}
} }
Loading…
Cancel
Save