Skip to content
Permalink
Browse files

Renaming exceptions to not use Error400, Error500. Instead HTTP statu…

…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 e5b4dd94d71d92392fe209219c44407af578fe85
Showing with 76 additions and 12 deletions.
  1. +4 −0 cake/libs/error_handler.php
  2. +64 −4 cake/libs/exceptions.php
  3. +8 −8 cake/tests/cases/libs/error_handler.test.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);
@@ -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);
}
@@ -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/<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();
@@ -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);

0 comments on commit e5b4dd9

Please sign in to comment.
You can’t perform that action at this time.