From c7c13a66937df578cbfcecc107b040eb19988eec Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Fri, 29 Nov 2013 21:22:08 -0500 Subject: [PATCH] Added concrete http exception classes. --- .../frontend/controllers/SiteController.php | 4 ++-- docs/guide/authorization.md | 6 ++--- docs/guide/controller.md | 2 +- extensions/debug/Module.php | 4 ++-- extensions/debug/controllers/DefaultController.php | 4 ++-- extensions/gii/Module.php | 4 ++-- extensions/gii/controllers/DefaultController.php | 14 +++++------ .../gii/generators/crud/templates/controller.php | 6 ++--- framework/yii/base/Application.php | 4 ++-- framework/yii/web/AccessControl.php | 4 ++-- framework/yii/web/AccessDeniedHttpException.php | 28 ++++++++++++++++++++++ framework/yii/web/Application.php | 4 ++-- framework/yii/web/BadRequestHttpException.php | 28 ++++++++++++++++++++++ framework/yii/web/Controller.php | 6 ++--- .../yii/web/MethodNotAllowedHttpException.php | 28 ++++++++++++++++++++++ framework/yii/web/NotFoundHttpException.php | 28 ++++++++++++++++++++++ framework/yii/web/Request.php | 2 +- framework/yii/web/User.php | 2 +- framework/yii/web/VerbFilter.php | 2 +- 19 files changed, 146 insertions(+), 34 deletions(-) create mode 100644 framework/yii/web/AccessDeniedHttpException.php create mode 100644 framework/yii/web/BadRequestHttpException.php create mode 100644 framework/yii/web/MethodNotAllowedHttpException.php create mode 100644 framework/yii/web/NotFoundHttpException.php diff --git a/apps/advanced/frontend/controllers/SiteController.php b/apps/advanced/frontend/controllers/SiteController.php index edb9cd9..db6dbe6 100644 --- a/apps/advanced/frontend/controllers/SiteController.php +++ b/apps/advanced/frontend/controllers/SiteController.php @@ -7,7 +7,7 @@ use yii\web\Controller; use common\models\LoginForm; use frontend\models\ContactForm; use common\models\User; -use yii\web\HttpException; +use yii\web\BadRequestHttpException; use yii\helpers\Security; class SiteController extends Controller @@ -132,7 +132,7 @@ class SiteController extends Controller ]); if (!$model) { - throw new HttpException(400, 'Wrong password reset token.'); + throw new BadRequestHttpException('Wrong password reset token.'); } $model->scenario = 'resetPassword'; diff --git a/docs/guide/authorization.md b/docs/guide/authorization.md index 8f2b97f..086aa32 100644 --- a/docs/guide/authorization.md +++ b/docs/guide/authorization.md @@ -234,10 +234,10 @@ public function editArticle($id) { $article = Article::find($id); if (!$article) { - throw new HttpException(404); + throw new NotFoundHttpException; } if (!\Yii::$app->user->checkAccess('edit_article', ['article' => $article])) { - throw new HttpException(403); + throw new AccessDeniedHttpException; } // ... } @@ -250,7 +250,7 @@ public function editArticle($id) { $article = Article::find(['id' => $id, 'author_id' => \Yii::$app->user->id]); if (!$article) { - throw new HttpException(404); + throw new NotFoundHttpException; } // ... } diff --git a/docs/guide/controller.md b/docs/guide/controller.md index 571a833..a28476b 100644 --- a/docs/guide/controller.md +++ b/docs/guide/controller.md @@ -122,7 +122,7 @@ class BlogController extends Controller { $post = Post::find($id); if (!$post) { - throw new HttpException(404); + throw new NotFoundHttpException; } if (\Yii::$app->request->isPost)) { diff --git a/extensions/debug/Module.php b/extensions/debug/Module.php index 51f69a6..8a20021 100644 --- a/extensions/debug/Module.php +++ b/extensions/debug/Module.php @@ -10,7 +10,7 @@ namespace yii\debug; use Yii; use yii\base\Application; use yii\web\View; -use yii\web\HttpException; +use yii\web\AccessDeniedHttpException; /** * The Yii Debug Module provides the debug toolbar and debugger @@ -79,7 +79,7 @@ class Module extends \yii\base\Module } elseif ($action->id === 'toolbar') { return false; } else { - throw new HttpException(403, 'You are not allowed to access this page.'); + throw new AccessDeniedHttpException('You are not allowed to access this page.'); } } diff --git a/extensions/debug/controllers/DefaultController.php b/extensions/debug/controllers/DefaultController.php index a4ac633..6694d26 100644 --- a/extensions/debug/controllers/DefaultController.php +++ b/extensions/debug/controllers/DefaultController.php @@ -9,7 +9,7 @@ namespace yii\debug\controllers; use Yii; use yii\web\Controller; -use yii\web\HttpException; +use yii\web\NotFoundHttpException; /** * @author Qiang Xue @@ -99,7 +99,7 @@ class DefaultController extends Controller } $this->summary = $data['summary']; } else { - throw new HttpException(404, "Unable to find debug data tagged with '$tag'."); + throw new NotFoundHttpException("Unable to find debug data tagged with '$tag'."); } } } diff --git a/extensions/gii/Module.php b/extensions/gii/Module.php index 7dc9590..a7bb3ed 100644 --- a/extensions/gii/Module.php +++ b/extensions/gii/Module.php @@ -8,7 +8,7 @@ namespace yii\gii; use Yii; -use yii\web\HttpException; +use yii\web\AccessDeniedHttpException; /** * This is the main module class for the Gii module. @@ -110,7 +110,7 @@ class Module extends \yii\base\Module if ($this->checkAccess()) { return parent::beforeAction($action); } else { - throw new HttpException(403, 'You are not allowed to access this page.'); + throw new AccessDeniedHttpException('You are not allowed to access this page.'); } } diff --git a/extensions/gii/controllers/DefaultController.php b/extensions/gii/controllers/DefaultController.php index ef104c3..2b3bf00 100644 --- a/extensions/gii/controllers/DefaultController.php +++ b/extensions/gii/controllers/DefaultController.php @@ -9,7 +9,7 @@ namespace yii\gii\controllers; use Yii; use yii\web\Controller; -use yii\web\HttpException; +use yii\web\NotFoundHttpException; /** * @author Qiang Xue @@ -69,7 +69,7 @@ class DefaultController extends Controller } } } - throw new HttpException(404, "Code file not found: $file"); + throw new NotFoundHttpException("Code file not found: $file"); } public function actionDiff($id, $file) @@ -84,7 +84,7 @@ class DefaultController extends Controller } } } - throw new HttpException(404, "Code file not found: $file"); + throw new NotFoundHttpException("Code file not found: $file"); } /** @@ -94,7 +94,7 @@ class DefaultController extends Controller * @param string $id the ID of the generator * @param string $name the action name * @return mixed the result of the action. - * @throws HttpException if the action method does not exist. + * @throws NotFoundHttpException if the action method does not exist. */ public function actionAction($id, $name) { @@ -103,7 +103,7 @@ class DefaultController extends Controller if (method_exists($generator, $method)) { return $generator->$method(); } else { - throw new HttpException(400, "Unknown generator action: $name"); + throw new NotFoundHttpException("Unknown generator action: $name"); } } @@ -136,7 +136,7 @@ class DefaultController extends Controller * Loads the generator with the specified ID. * @param string $id the ID of the generator to be loaded. * @return \yii\gii\Generator the loaded generator - * @throws \yii\web\HttpException + * @throws NotFoundHttpException */ protected function loadGenerator($id) { @@ -146,7 +146,7 @@ class DefaultController extends Controller $this->generator->load($_POST); return $this->generator; } else { - throw new HttpException(404, "Code generator not found: $id"); + throw new NotFoundHttpException("Code generator not found: $id"); } } } diff --git a/extensions/gii/generators/crud/templates/controller.php b/extensions/gii/generators/crud/templates/controller.php index f975fd1..4490cd0 100644 --- a/extensions/gii/generators/crud/templates/controller.php +++ b/extensions/gii/generators/crud/templates/controller.php @@ -29,7 +29,7 @@ namespace controllerClass, '\\')) ?> use modelClass, '\\') ?>; use searchModelClass, '\\') ?> as ; use baseControllerClass, '\\') ?>; -use yii\web\HttpException; +use yii\web\NotFoundHttpException; use yii\web\VerbFilter; /** @@ -130,7 +130,7 @@ class extends bas * If the model is not found, a 404 HTTP exception will be thrown. * * @return the loaded model - * @throws HttpException if the model cannot be found + * @throws NotFoundHttpException if the model cannot be found */ protected function findModel() { @@ -148,7 +148,7 @@ if (count($pks) === 1) { if (($model = ::find()) !== null) { return $model; } else { - throw new HttpException(404, 'The requested page does not exist.'); + throw new NotFoundHttpException('The requested page does not exist.'); } } } diff --git a/framework/yii/base/Application.php b/framework/yii/base/Application.php index 756d4e2..3e176f4 100644 --- a/framework/yii/base/Application.php +++ b/framework/yii/base/Application.php @@ -634,9 +634,9 @@ abstract class Application extends Module { $category = get_class($exception); if ($exception instanceof HttpException) { - $category .= '\\' . $exception->statusCode; + $category = 'yii\\web\\HttpException:' . $exception->statusCode; } elseif ($exception instanceof \ErrorException) { - $category .= '\\' . $exception->getSeverity(); + $category .= ':' . $exception->getSeverity(); } Yii::error((string)$exception, $category); } diff --git a/framework/yii/web/AccessControl.php b/framework/yii/web/AccessControl.php index 549f087..e755e80 100644 --- a/framework/yii/web/AccessControl.php +++ b/framework/yii/web/AccessControl.php @@ -131,14 +131,14 @@ class AccessControl extends ActionFilter * The default implementation will redirect the user to the login page if he is a guest; * if the user is already logged, a 403 HTTP exception will be thrown. * @param User $user the current user - * @throws HttpException if the user is already logged in. + * @throws AccessDeniedHttpException if the user is already logged in. */ protected function denyAccess($user) { if ($user->getIsGuest()) { $user->loginRequired(); } else { - throw new HttpException(403, Yii::t('yii', 'You are not allowed to perform this action.')); + throw new AccessDeniedHttpException(Yii::t('yii', 'You are not allowed to perform this action.')); } } } diff --git a/framework/yii/web/AccessDeniedHttpException.php b/framework/yii/web/AccessDeniedHttpException.php new file mode 100644 index 0000000..d83700b --- /dev/null +++ b/framework/yii/web/AccessDeniedHttpException.php @@ -0,0 +1,28 @@ + + * @since 2.0 + */ +class AccessDeniedHttpException extends HttpException +{ + /** + * Constructor. + * @param string $message error message + * @param integer $code error code + * @param \Exception $previous The previous exception used for the exception chaining. + */ + public function __construct($message = null, $code = 0, \Exception $previous = null) + { + parent::__construct(403, $message, $code, $previous); + } +} diff --git a/framework/yii/web/Application.php b/framework/yii/web/Application.php index 13b5588..17c1411 100644 --- a/framework/yii/web/Application.php +++ b/framework/yii/web/Application.php @@ -58,7 +58,7 @@ class Application extends \yii\base\Application * Handles the specified request. * @param Request $request the request to be handled * @return Response the resulting response - * @throws HttpException if the requested route is invalid + * @throws NotFoundHttpException if the requested route is invalid */ public function handleRequest($request) { @@ -85,7 +85,7 @@ class Application extends \yii\base\Application return $response; } } catch (InvalidRouteException $e) { - throw new HttpException(404, $e->getMessage(), $e->getCode(), $e); + throw new NotFoundHttpException($e->getMessage(), $e->getCode(), $e); } } diff --git a/framework/yii/web/BadRequestHttpException.php b/framework/yii/web/BadRequestHttpException.php new file mode 100644 index 0000000..3a6cfbb --- /dev/null +++ b/framework/yii/web/BadRequestHttpException.php @@ -0,0 +1,28 @@ + + * @since 2.0 + */ +class BadRequestHttpException extends HttpException +{ + /** + * Constructor. + * @param string $message error message + * @param integer $code error code + * @param \Exception $previous The previous exception used for the exception chaining. + */ + public function __construct($message = null, $code = 0, \Exception $previous = null) + { + parent::__construct(400, $message, $code, $previous); + } +} diff --git a/framework/yii/web/Controller.php b/framework/yii/web/Controller.php index e01a924..0df48bd 100644 --- a/framework/yii/web/Controller.php +++ b/framework/yii/web/Controller.php @@ -62,7 +62,7 @@ class Controller extends \yii\base\Controller } elseif (!is_array($params[$name])) { $args[] = $actionParams[$name] = $params[$name]; } else { - throw new HttpException(400, Yii::t('yii', 'Invalid data received for parameter "{param}".', [ + throw new BadRequestHttpException(Yii::t('yii', 'Invalid data received for parameter "{param}".', [ 'param' => $name, ])); } @@ -75,7 +75,7 @@ class Controller extends \yii\base\Controller } if (!empty($missing)) { - throw new HttpException(400, Yii::t('yii', 'Missing required parameters: {params}', [ + throw new BadRequestHttpException(Yii::t('yii', 'Missing required parameters: {params}', [ 'params' => implode(', ', $missing), ])); } @@ -92,7 +92,7 @@ class Controller extends \yii\base\Controller { if (parent::beforeAction($action)) { if ($this->enableCsrfValidation && Yii::$app->exception === null && !Yii::$app->getRequest()->validateCsrfToken()) { - throw new HttpException(400, Yii::t('yii', 'Unable to verify your data submission.')); + throw new BadRequestHttpException(Yii::t('yii', 'Unable to verify your data submission.')); } return true; } else { diff --git a/framework/yii/web/MethodNotAllowedHttpException.php b/framework/yii/web/MethodNotAllowedHttpException.php new file mode 100644 index 0000000..d894f57 --- /dev/null +++ b/framework/yii/web/MethodNotAllowedHttpException.php @@ -0,0 +1,28 @@ + + * @since 2.0 + */ +class MethodNotAllowedHttpException extends HttpException +{ + /** + * Constructor. + * @param string $message error message + * @param integer $code error code + * @param \Exception $previous The previous exception used for the exception chaining. + */ + public function __construct($message = null, $code = 0, \Exception $previous = null) + { + parent::__construct(405, $message, $code, $previous); + } +} diff --git a/framework/yii/web/NotFoundHttpException.php b/framework/yii/web/NotFoundHttpException.php new file mode 100644 index 0000000..71f246d --- /dev/null +++ b/framework/yii/web/NotFoundHttpException.php @@ -0,0 +1,28 @@ + + * @since 2.0 + */ +class NotFoundHttpException extends HttpException +{ + /** + * Constructor. + * @param string $message error message + * @param integer $code error code + * @param \Exception $previous The previous exception used for the exception chaining. + */ + public function __construct($message = null, $code = 0, \Exception $previous = null) + { + parent::__construct(404, $message, $code, $previous); + } +} diff --git a/framework/yii/web/Request.php b/framework/yii/web/Request.php index 04bf0e3..9736043 100644 --- a/framework/yii/web/Request.php +++ b/framework/yii/web/Request.php @@ -139,7 +139,7 @@ class Request extends \yii\base\Request $_GET = array_merge($_GET, $params); return [$route, $_GET]; } else { - throw new HttpException(404, Yii::t('yii', 'Page not found.')); + throw new NotFoundHttpException(Yii::t('yii', 'Page not found.')); } } diff --git a/framework/yii/web/User.php b/framework/yii/web/User.php index 682d78e..b640756 100644 --- a/framework/yii/web/User.php +++ b/framework/yii/web/User.php @@ -331,7 +331,7 @@ class User extends Component Yii::$app->getResponse()->redirect($this->loginUrl)->send(); exit(); } else { - throw new HttpException(403, Yii::t('yii', 'Login Required')); + throw new AccessDeniedHttpException(Yii::t('yii', 'Login Required')); } } diff --git a/framework/yii/web/VerbFilter.php b/framework/yii/web/VerbFilter.php index e673bae..65d182b 100644 --- a/framework/yii/web/VerbFilter.php +++ b/framework/yii/web/VerbFilter.php @@ -100,7 +100,7 @@ class VerbFilter extends Behavior $event->isValid = false; // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.7 Yii::$app->getResponse()->getHeaders()->set('Allow', implode(', ', $allowed)); - throw new HttpException(405, Yii::t('yii', 'Method Not Allowed. This url can only handle the following request methods: {methods}.', [ + throw new MethodNotAllowedHttpException(Yii::t('yii', 'Method Not Allowed. This url can only handle the following request methods: {methods}.', [ 'methods' => implode(', ', $allowed), ])); }