Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Allowing plugins to offer ExceptionRenderer classes
  • Loading branch information
lorenzo committed Apr 24, 2011
1 parent 24fbaaf commit dfb669a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
12 changes: 7 additions & 5 deletions lib/Cake/Error/ErrorHandler.php
Expand Up @@ -54,13 +54,13 @@
*
* This controller method is called instead of the default exception rendering. It receives the
* thrown exception as its only argument. You should implement your error handling in that method.
* Using AppController::appError(), will superseed any configuration for Exception.renderer.
* Using AppController::appError(), will supersede any configuration for Exception.renderer.
*
* #### Using a custom renderer with `Exception.renderer`
*
* If you don't want to take control of the exception handling, but want to change how exceptions are
* rendered you can use `Exception.renderer` to choose a class to render exception pages. By default
* `ExceptionRenderer` is used. Your custom exception renderer class should be placed in app/libs.
* `ExceptionRenderer` is used. Your custom exception renderer class should be placed in app/Lib/Error.
*
* Your custom renderer should expect an exception in its constructor, and implement a render method.
* Failing to do so will cause additional errors.
Expand Down Expand Up @@ -116,10 +116,12 @@ public static function handleException(Exception $exception) {
);
CakeLog::write(LOG_ERR, $message);
}
if ($config['renderer'] !== 'ExceptionRenderer') {
App::uses($config['renderer'], 'Error');
$renderer = $config['renderer'];
if ($renderer !== 'ExceptionRenderer') {
list($plugin, $renderer) = pluginSplit($renderer, true);
App::uses($renderer, $plugin . 'Error');
}
$error = new $config['renderer']($exception);
$error = new $renderer($exception);
$error->render();
}

Expand Down
22 changes: 20 additions & 2 deletions lib/Cake/tests/Case/Error/ErrorHandlerTest.php
Expand Up @@ -37,8 +37,7 @@ class ErrorHandlerTest extends CakeTestCase {
function setUp() {
App::build(array(
'View' => array(
LIBS . 'tests' . DS . 'test_app' . DS . 'views'. DS,
LIBS . 'libs' . DS . 'view' . DS
LIBS . 'tests' . DS . 'test_app' . DS . 'View'. DS
)
), true);
Router::reload();
Expand Down Expand Up @@ -223,4 +222,23 @@ function testHandleExceptionLog() {
$this->assertPattern('/\#0.*ErrorHandlerTest->testHandleExceptionLog/', $log[1], 'Stack trace missing.');
}

/**
* tests it is possible to load a plugin exception renderer
*
* @return void
*/
function testLoadPluginHanlder() {
App::build(array(
'plugins' => array(
LIBS . 'tests' . DS . 'test_app' . DS . 'plugins' . DS
)
), true);
Configure::write('Exception.renderer', 'TestPlugin.TestPluginExceptionRenderer');
$error = new NotFoundException('Kaboom!');
ob_start();
ErrorHandler::handleException($error);
$result = ob_get_clean();
$this->assertEquals($result, 'Rendered by test plugin');
}

}

0 comments on commit dfb669a

Please sign in to comment.