Allowing plugins to offer ExceptionRenderer classes

This commit is contained in:
Jose Lorenzo Rodriguez 2011-04-24 00:56:48 -04:30
parent 24fbaafe01
commit dfb669ab8e
2 changed files with 27 additions and 7 deletions

View file

@ -54,13 +54,13 @@ App::uses('AppController', 'Controller');
* *
* This controller method is called instead of the default exception rendering. It receives the * 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. * 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` * #### 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 * 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 * 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. * Your custom renderer should expect an exception in its constructor, and implement a render method.
* Failing to do so will cause additional errors. * Failing to do so will cause additional errors.
@ -116,10 +116,12 @@ class ErrorHandler {
); );
CakeLog::write(LOG_ERR, $message); CakeLog::write(LOG_ERR, $message);
} }
if ($config['renderer'] !== 'ExceptionRenderer') { $renderer = $config['renderer'];
App::uses($config['renderer'], 'Error'); 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(); $error->render();
} }

View file

@ -37,8 +37,7 @@ class ErrorHandlerTest extends CakeTestCase {
function setUp() { function setUp() {
App::build(array( App::build(array(
'View' => array( 'View' => array(
LIBS . 'tests' . DS . 'test_app' . DS . 'views'. DS, LIBS . 'tests' . DS . 'test_app' . DS . 'View'. DS
LIBS . 'libs' . DS . 'view' . DS
) )
), true); ), true);
Router::reload(); Router::reload();
@ -223,4 +222,23 @@ class ErrorHandlerTest extends CakeTestCase {
$this->assertPattern('/\#0.*ErrorHandlerTest->testHandleExceptionLog/', $log[1], 'Stack trace missing.'); $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');
}
} }