浏览代码

Allowing plugins to offer ExceptionRenderer classes

Jose Lorenzo Rodriguez 15 年之前
父节点
当前提交
dfb669ab8e
共有 2 个文件被更改,包括 27 次插入7 次删除
  1. 7 5
      lib/Cake/Error/ErrorHandler.php
  2. 20 2
      lib/Cake/tests/Case/Error/ErrorHandlerTest.php

+ 7 - 5
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();
 	}
 

+ 20 - 2
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');
+	}
+
 }