From ad218c6719e097f4bd284872fa023cb97e737941 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Mon, 10 Jun 2013 17:08:26 -0400 Subject: [PATCH] moved HttpException to web. --- framework/yii/base/Application.php | 1 + framework/yii/base/ErrorHandler.php | 1 + framework/yii/base/HttpException.php | 52 ----------------------------- framework/yii/views/errorHandler/main.php | 4 +-- framework/yii/web/AccessControl.php | 2 +- framework/yii/web/Application.php | 2 +- framework/yii/web/Controller.php | 2 +- framework/yii/web/HttpException.php | 54 +++++++++++++++++++++++++++++++ framework/yii/web/Request.php | 2 +- framework/yii/web/Response.php | 2 +- framework/yii/web/User.php | 2 +- framework/yii/web/VerbFilter.php | 4 +-- tests/unit/framework/web/ResponseTest.php | 2 +- 13 files changed, 67 insertions(+), 63 deletions(-) delete mode 100644 framework/yii/base/HttpException.php create mode 100644 framework/yii/web/HttpException.php diff --git a/framework/yii/base/Application.php b/framework/yii/base/Application.php index 8a96e94..9969ecd 100644 --- a/framework/yii/base/Application.php +++ b/framework/yii/base/Application.php @@ -8,6 +8,7 @@ namespace yii\base; use Yii; +use yii\web\HttpException; /** * Application is the base class for all application classes. diff --git a/framework/yii/base/ErrorHandler.php b/framework/yii/base/ErrorHandler.php index 4e3e92a..fe9eef3 100644 --- a/framework/yii/base/ErrorHandler.php +++ b/framework/yii/base/ErrorHandler.php @@ -8,6 +8,7 @@ namespace yii\base; use Yii; +use yii\web\HttpException; /** * ErrorHandler handles uncaught PHP errors and exceptions. diff --git a/framework/yii/base/HttpException.php b/framework/yii/base/HttpException.php deleted file mode 100644 index 2e3c8d4..0000000 --- a/framework/yii/base/HttpException.php +++ /dev/null @@ -1,52 +0,0 @@ - - * @since 2.0 - */ -class HttpException extends UserException -{ - /** - * @var integer HTTP status code, such as 403, 404, 500, etc. - */ - public $statusCode; - - /** - * Constructor. - * @param integer $status HTTP status code, such as 404, 500, etc. - * @param string $message error message - * @param integer $code error code - * @param \Exception $previous The previous exception used for the exception chaining. - */ - public function __construct($status, $message = null, $code = 0, \Exception $previous = null) - { - $this->statusCode = $status; - parent::__construct($message, $code, $previous); - } - - /** - * @return string the user-friendly name of this exception - */ - public function getName() - { - // use absolute namespaced class here because PHP will generate a mysterious error otherwise - if (isset(\yii\web\Response::$httpStatuses[$this->statusCode])) { - return \yii\web\Response::$httpStatuses[$this->statusCode]; - } else { - return 'Error'; - } - } -} diff --git a/framework/yii/views/errorHandler/main.php b/framework/yii/views/errorHandler/main.php index d7bbb3d..05e217e 100644 --- a/framework/yii/views/errorHandler/main.php +++ b/framework/yii/views/errorHandler/main.php @@ -14,7 +14,7 @@ $context = $this->context; <?php - if ($exception instanceof \yii\base\HttpException) { + if ($exception instanceof \yii\web\HttpException) { echo (int) $exception->statusCode . ' ' . $context->htmlEncode($exception->getName()); } elseif ($exception instanceof \yii\base\Exception) { echo $context->htmlEncode($exception->getName() . ' – ' . get_class($exception)); @@ -362,7 +362,7 @@ pre .diff .change{ <?php else: ?> <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAAA6CAIAAACPssguAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyBpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMC1jMDYwIDYxLjEzNDc3NywgMjAxMC8wMi8xMi0xNzozMjowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4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wzyefv+/dRdG86/zItzvri+7knS3ZLefPgQPX7sMxSUy+W8ly+NtbW7Ir2+tjaoVHL5fDqdFgFENpstlMtQ/bdlOc3pJZLpjX6x6G1t/ff0KegdH1v96yV7EEk7nU6r1bJtm+PrVPx9FyFQd7FYBMa+sLAwBODnMgirAAM+p/EqLDcuJAiwbaCsgU+fsf8vwADjle3ME1OGEwAAAABJRU5ErkJggg==" alt="Attention"/> <h1><?php - if ($exception instanceof \yii\base\HttpException) { + if ($exception instanceof \yii\web\HttpException) { echo '<span>' . $context->createHttpStatusLink($exception->statusCode, $context->htmlEncode($exception->getName())) . '</span>'; echo ' – ' . $context->addTypeLinks(get_class($exception)); } elseif ($exception instanceof \yii\base\Exception) { diff --git a/framework/yii/web/AccessControl.php b/framework/yii/web/AccessControl.php index ce64533..7dedaf9 100644 --- a/framework/yii/web/AccessControl.php +++ b/framework/yii/web/AccessControl.php @@ -10,7 +10,7 @@ namespace yii\web; use Yii; use yii\base\Action; use yii\base\ActionFilter; -use yii\base\HttpException; +use yii\web\HttpException; /** * AccessControl provides simple access control based on a set of rules. diff --git a/framework/yii/web/Application.php b/framework/yii/web/Application.php index 12c9295..ce326a2 100644 --- a/framework/yii/web/Application.php +++ b/framework/yii/web/Application.php @@ -8,7 +8,7 @@ namespace yii\web; use Yii; -use yii\base\HttpException; +use yii\web\HttpException; use yii\base\InvalidRouteException; /** diff --git a/framework/yii/web/Controller.php b/framework/yii/web/Controller.php index 026c078..22a2ebd 100644 --- a/framework/yii/web/Controller.php +++ b/framework/yii/web/Controller.php @@ -8,7 +8,7 @@ namespace yii\web; use Yii; -use yii\base\HttpException; +use yii\web\HttpException; use yii\base\InlineAction; /** diff --git a/framework/yii/web/HttpException.php b/framework/yii/web/HttpException.php new file mode 100644 index 0000000..384a5b4 --- /dev/null +++ b/framework/yii/web/HttpException.php @@ -0,0 +1,54 @@ +<?php +/** + * @link http://www.yiiframework.com/ + * @copyright Copyright (c) 2008 Yii Software LLC + * @license http://www.yiiframework.com/license/ + */ + +namespace yii\web; + +use yii\base\UserException; +use yii\web\Response; + +/** + * HttpException represents an exception caused by an improper request of the end-user. + * + * HttpException can be differentiated via its [[statusCode]] property value which + * keeps a standard HTTP status code (e.g. 404, 500). Error handlers may use this status code + * to decide how to format the error page. + * + * @author Qiang Xue <qiang.xue@gmail.com> + * @since 2.0 + */ +class HttpException extends UserException +{ + /** + * @var integer HTTP status code, such as 403, 404, 500, etc. + */ + public $statusCode; + + /** + * Constructor. + * @param integer $status HTTP status code, such as 404, 500, etc. + * @param string $message error message + * @param integer $code error code + * @param \Exception $previous The previous exception used for the exception chaining. + */ + public function __construct($status, $message = null, $code = 0, \Exception $previous = null) + { + $this->statusCode = $status; + parent::__construct($message, $code, $previous); + } + + /** + * @return string the user-friendly name of this exception + */ + public function getName() + { + if (isset(Response::$httpStatuses[$this->statusCode])) { + return Response::$httpStatuses[$this->statusCode]; + } else { + return 'Error'; + } + } +} diff --git a/framework/yii/web/Request.php b/framework/yii/web/Request.php index 6f5cdb5..afd2f8a 100644 --- a/framework/yii/web/Request.php +++ b/framework/yii/web/Request.php @@ -8,7 +8,7 @@ namespace yii\web; use Yii; -use yii\base\HttpException; +use yii\web\HttpException; use yii\base\InvalidConfigException; use yii\helpers\SecurityHelper; diff --git a/framework/yii/web/Response.php b/framework/yii/web/Response.php index d74ef2c..ac1c9cc 100644 --- a/framework/yii/web/Response.php +++ b/framework/yii/web/Response.php @@ -8,7 +8,7 @@ namespace yii\web; use Yii; -use yii\base\HttpException; +use yii\web\HttpException; use yii\base\InvalidParamException; use yii\helpers\FileHelper; use yii\helpers\Html; diff --git a/framework/yii/web/User.php b/framework/yii/web/User.php index 7ea561c..f273c1a 100644 --- a/framework/yii/web/User.php +++ b/framework/yii/web/User.php @@ -9,7 +9,7 @@ namespace yii\web; use Yii; use yii\base\Component; -use yii\base\HttpException; +use yii\web\HttpException; use yii\base\InvalidConfigException; /** diff --git a/framework/yii/web/VerbFilter.php b/framework/yii/web/VerbFilter.php index 2b7567f..a3fd662 100644 --- a/framework/yii/web/VerbFilter.php +++ b/framework/yii/web/VerbFilter.php @@ -10,7 +10,7 @@ namespace yii\web; use Yii; use yii\base\ActionEvent; use yii\base\Behavior; -use yii\base\HttpException; +use yii\web\HttpException; /** * VerbFilter is an action filter that filters by HTTP request methods. @@ -70,7 +70,7 @@ class VerbFilter extends Behavior /** * @param ActionEvent $event * @return boolean - * @throws \yii\base\HttpException when the request method is not allowed. + * @throws HttpException when the request method is not allowed. */ public function beforeAction($event) { diff --git a/tests/unit/framework/web/ResponseTest.php b/tests/unit/framework/web/ResponseTest.php index d87546f..f35dda4 100644 --- a/tests/unit/framework/web/ResponseTest.php +++ b/tests/unit/framework/web/ResponseTest.php @@ -76,7 +76,7 @@ class ResponseTest extends \yiiunit\TestCase */ public function testSendFileWrongRanges($rangeHeader) { - $this->setExpectedException('yii\base\HttpException'); + $this->setExpectedException('yii\web\HttpException'); $dataFile = \Yii::getAlias('@yiiunit/data/web/data.txt'); $_SERVER['HTTP_RANGE'] = 'bytes=' . $rangeHeader;