Skip to content

Commit

Permalink
Renaming exceptions to not use Error400, Error500. Instead HTTP statu…
Browse files Browse the repository at this point in the history
…s words are used.

Adding more common HTTP status errors.
Updating tests for ErrorHandler.
  • Loading branch information
markstory committed Sep 5, 2010
1 parent b7791dd commit e5b4dd9
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 12 deletions.
4 changes: 4 additions & 0 deletions cake/libs/error_handler.php
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
68 changes: 64 additions & 4 deletions cake/libs/exceptions.php
Expand Up @@ -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
*
Expand All @@ -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);
}
Expand All @@ -44,7 +104,7 @@ public function __construct($message, $code = 404) {
*
* @package cake.libs
*/
class Error500Exception extends CakeException {
class InternalErrorException extends CakeException {
/**
* Constructor
*
Expand All @@ -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);
}
Expand Down
16 changes: 8 additions & 8 deletions cake/tests/cases/libs/error_handler.test.php
Expand Up @@ -193,7 +193,7 @@ function missingWidgetThing() {
*
* @package cake.test.cases.libs
*/
class MissingWidgetThingException extends Error404Exception { }
class MissingWidgetThingException extends NotFoundException { }


/**
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -429,13 +429,13 @@ function testerror400OnlyChangingCakeException() {
*
* @return void
*/
function testerror400NoInjection() {
function testError400NoInjection() {
Router::reload();

$request = new CakeRequest('pages/<span id=333>pink</span></id><script>document.body.style.background = t=document.getElementById(333).innerHTML;window.alert(t);</script>', false);
Router::setRequestInfo($request);

$exception = new Error404Exception('Custom message');
$exception = new NotFoundException('Custom message');
$ErrorHandler = new ErrorHandler($exception);

ob_start();
Expand All @@ -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);
Expand Down

0 comments on commit e5b4dd9

Please sign in to comment.