From dfb669ab8ed18c54a5e8f32a2aff84edaca34b85 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Sun, 24 Apr 2011 00:56:48 -0430 Subject: [PATCH] Allowing plugins to offer ExceptionRenderer classes --- lib/Cake/Error/ErrorHandler.php | 12 +++++----- .../tests/Case/Error/ErrorHandlerTest.php | 22 +++++++++++++++++-- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/lib/Cake/Error/ErrorHandler.php b/lib/Cake/Error/ErrorHandler.php index 6787e23f7..3bd2ef042 100644 --- a/lib/Cake/Error/ErrorHandler.php +++ b/lib/Cake/Error/ErrorHandler.php @@ -54,13 +54,13 @@ App::uses('AppController', 'Controller'); * * 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. @@ -116,10 +116,12 @@ class ErrorHandler { ); 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(); } diff --git a/lib/Cake/tests/Case/Error/ErrorHandlerTest.php b/lib/Cake/tests/Case/Error/ErrorHandlerTest.php index 7fc875aea..bdb384cb9 100644 --- a/lib/Cake/tests/Case/Error/ErrorHandlerTest.php +++ b/lib/Cake/tests/Case/Error/ErrorHandlerTest.php @@ -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(); @@ -223,4 +222,23 @@ class ErrorHandlerTest extends CakeTestCase { $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'); + } + }