Navigation Menu

Skip to content

Commit

Permalink
Extracting a few methods to make testing easier, and subclasses easie…
Browse files Browse the repository at this point in the history
…r to create.
  • Loading branch information
markstory committed Aug 28, 2010
1 parent 26f4ad4 commit 5d942ee
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
46 changes: 36 additions & 10 deletions cake/libs/error.php
Expand Up @@ -47,17 +47,9 @@ class ErrorHandler {
* @param array $messages Error messages
*/
function __construct(Exception $exception) {
static $__previousError = null;
App::import('Core', 'Sanitize');
App::import('Controller', 'CakeError');

if ($__previousError != $exception) {
$__previousError = $exception;
$this->controller = new CakeErrorController();
} else {
$this->controller = new Controller();
$this->controller->viewPath = 'errors';
}
$this->controller = $this->_getController($exception);

if (method_exists($this->controller, 'apperror')) {
return $this->controller->appError($exception);
Expand All @@ -82,7 +74,31 @@ function __construct(Exception $exception) {
}
}
}
call_user_func_array(array($this, $method), array($exception));
$this->method = $method;
$this->error = $exception;
}

/**
* Get the controller instance to handle the exception.
* Override this method in subclasses to customize the controller used.
* This method returns the built in `CakeErrorController` normally, or if an error is repeated
* a bare controller will be used.
*
* @param Exception $exception The exception to get a controller for.
* @return Controller
*/
protected function _getController($exception) {
static $__previousError = null;
App::import('Controller', 'CakeError');

if ($__previousError != $exception) {
$__previousError = $exception;
$controller = new CakeErrorController();
} else {
$controller = new Controller();
$controller->viewPath = 'errors';
}
return $controller;
}

/**
Expand All @@ -95,6 +111,16 @@ function __construct(Exception $exception) {
*/
public static function handleException(Exception $exception) {
$error = new ErrorHandler($exception);
$error->render();
}

/**
* Renders the response for the exception.
*
* @return void
*/
public function render() {
call_user_func_array(array($this, $this->method), array($this->error));
}

/**
Expand Down
5 changes: 3 additions & 2 deletions cake/tests/cases/libs/error.test.php
Expand Up @@ -321,10 +321,11 @@ function testSubclassMethodsNotBeingConvertedToError() {
* @return void
*/
function testError() {
$this->markTestIncomplete('Not implemented now');
$exception = new Error404Exception('Page not found');
ob_start();
$TestErrorHandler = new TestErrorHandler('error404', array('message' => 'Page not found'));
$TestErrorHandler = new TestErrorHandler($exception);
ob_clean();

ob_start();
$TestErrorHandler->error(array(
'code' => 404,
Expand Down

0 comments on commit 5d942ee

Please sign in to comment.