From e5b4dd94d71d92392fe209219c44407af578fe85 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sun, 5 Sep 2010 02:05:31 -0400 Subject: [PATCH] Renaming exceptions to not use Error400, Error500. Instead HTTP status words are used. Adding more common HTTP status errors. Updating tests for ErrorHandler. --- cake/libs/error_handler.php | 4 ++ cake/libs/exceptions.php | 68 ++++++++++++++++++-- cake/tests/cases/libs/error_handler.test.php | 16 ++--- 3 files changed, 76 insertions(+), 12 deletions(-) diff --git a/cake/libs/error_handler.php b/cake/libs/error_handler.php index 2b2277521cb..15fd4b10192 100644 --- a/cake/libs/error_handler.php +++ b/cake/libs/error_handler.php @@ -106,6 +106,9 @@ function __construct(Exception $exception) { if ($exception instanceof CakeException && !$methodExists) { $method = '_cakeError'; + if ($template == 'internalError') { + $template = 'error500'; + } } elseif (!$methodExists) { $method = 'error500'; if ($code >= 400) { @@ -197,6 +200,7 @@ protected function _cakeError(CakeException $error) { $this->controller->set(array( 'code' => $code, 'url' => h($url), + 'name' => $error->getMessage() )); $this->controller->set($error->getAttributes()); $this->_outputMessage($this->template); diff --git a/cake/libs/exceptions.php b/cake/libs/exceptions.php index 817dc2d2041..962c83195c8 100644 --- a/cake/libs/exceptions.php +++ b/cake/libs/exceptions.php @@ -19,12 +19,72 @@ * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ +/** + * Represents an HTTP 400 error. + * + * @package cake.libs + */ +class BadRequestException extends RuntimeException { +/** + * Constructor + * + * @param string $message If no message is given 'Bad Request' will be the message + * @param string $code Status code, defaults to 401 + */ + public function __construct($message, $code = 400) { + if (empty($message)) { + $message = 'Bad Request'; + } + parent::__construct($message, $code); + } +} + +/** + * Represents an HTTP 401 error. + * + * @package cake.libs + */ +class UnauthorizedException extends RuntimeException { +/** + * Constructor + * + * @param string $message If no message is given 'Unauthorized' will be the message + * @param string $code Status code, defaults to 401 + */ + public function __construct($message, $code = 401) { + if (empty($message)) { + $message = 'Unauthorized'; + } + parent::__construct($message, $code); + } +} + +/** + * Represents an HTTP 403 error. + * + * @package cake.libs + */ +class ForbiddenException extends RuntimeException { +/** + * Constructor + * + * @param string $message If no message is given 'Forbidden' will be the message + * @param string $code Status code, defaults to 401 + */ + public function __construct($message, $code = 403) { + if (empty($message)) { + $message = 'Forbidden'; + } + parent::__construct($message, $code); + } +} + /** * Represents an HTTP 404 error. * * @package cake.libs */ -class Error404Exception extends RuntimeException { +class NotFoundException extends RuntimeException { /** * Constructor * @@ -33,7 +93,7 @@ class Error404Exception extends RuntimeException { */ public function __construct($message, $code = 404) { if (empty($message)) { - $message = __('Not Found'); + $message = 'Not Found'; } parent::__construct($message, $code); } @@ -44,7 +104,7 @@ public function __construct($message, $code = 404) { * * @package cake.libs */ -class Error500Exception extends CakeException { +class InternalErrorException extends CakeException { /** * Constructor * @@ -53,7 +113,7 @@ class Error500Exception extends CakeException { */ public function __construct($message, $code = 500) { if (empty($message)) { - $message = __('Internal Server Error'); + $message = 'Internal Server Error'; } parent::__construct($message, $code); } diff --git a/cake/tests/cases/libs/error_handler.test.php b/cake/tests/cases/libs/error_handler.test.php index 7915c14bc23..a57de787d4b 100644 --- a/cake/tests/cases/libs/error_handler.test.php +++ b/cake/tests/cases/libs/error_handler.test.php @@ -193,7 +193,7 @@ function missingWidgetThing() { * * @package cake.test.cases.libs */ -class MissingWidgetThingException extends Error404Exception { } +class MissingWidgetThingException extends NotFoundException { } /** @@ -239,7 +239,7 @@ function testHandleException() { if ($this->skipIf(file_exists(APP . 'app_error.php'), 'App error exists cannot run.')) { return; } - $error = new Error404Exception('Kaboom!'); + $error = new NotFoundException('Kaboom!'); ob_start(); ErrorHandler::handleException($error); $result = ob_get_clean(); @@ -310,7 +310,7 @@ function testSubclassConvertingFrameworkErrors() { * @return void */ function testConstruction() { - $exception = new Error404Exception('Page not found'); + $exception = new NotFoundException('Page not found'); $ErrorHandler = new ErrorHandler($exception); $this->assertType('CakeErrorController', $ErrorHandler->controller); @@ -385,7 +385,7 @@ function testerror400() { $request = new CakeRequest('posts/view/1000', false); Router::setRequestInfo($request); - $exception = new Error404Exception('Custom message'); + $exception = new NotFoundException('Custom message'); $ErrorHandler = new ErrorHandler($exception); $ErrorHandler->controller->response = $this->getMock('CakeResponse', array('statusCode')); $ErrorHandler->controller->response->expects($this->once())->method('statusCode')->with(404); @@ -408,7 +408,7 @@ function testerror400() { function testerror400OnlyChangingCakeException() { Configure::write('debug', 0); - $exception = new Error404Exception('Custom message'); + $exception = new NotFoundException('Custom message'); $ErrorHandler = new ErrorHandler($exception); ob_start(); @@ -429,13 +429,13 @@ function testerror400OnlyChangingCakeException() { * * @return void */ - function testerror400NoInjection() { + function testError400NoInjection() { Router::reload(); $request = new CakeRequest('pages/pink', false); Router::setRequestInfo($request); - $exception = new Error404Exception('Custom message'); + $exception = new NotFoundException('Custom message'); $ErrorHandler = new ErrorHandler($exception); ob_start(); @@ -453,7 +453,7 @@ function testerror400NoInjection() { * @return void */ function testError500Message() { - $exception = new Error500Exception('An Internal Error Has Occurred'); + $exception = new InternalErrorException('An Internal Error Has Occurred'); $ErrorHandler = new ErrorHandler($exception); $ErrorHandler->controller->response = $this->getMock('CakeResponse', array('statusCode')); $ErrorHandler->controller->response->expects($this->once())->method('statusCode')->with(500);